<!DOCTYPE html><html><head>
      <title>并发编程</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:///c:\Users\Unwarierbl\.vscode\extensions\shd101wyy.markdown-preview-enhanced-0.8.15\crossnote\dependencies\katex\katex.min.css">
      
      
      
      
      
      <style>
      code[class*=language-],
pre[class*=language-] {
    color: #333;
    background: 0 0;
    font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
    text-align: left;
    white-space: pre;
    word-spacing: normal;
    word-break: normal;
    word-wrap: normal;
    line-height: 1.4;
    -moz-tab-size: 8;
    -o-tab-size: 8;
    tab-size: 8;
    -webkit-hyphens: none;
    -moz-hyphens: none;
    -ms-hyphens: none;
    hyphens: none
}

pre[class*=language-] {
    padding: .8em;
    overflow: auto;
    border-radius: 3px;
    background: #f5f5f5
}

:not(pre)>code[class*=language-] {
    padding: .1em;
    border-radius: .3em;
    white-space: normal;
    background: #f5f5f5
}

.token.blockquote,
.token.comment {
    color: #969896
}

.token.cdata {
    color: #183691
}

.token.doctype,
.token.macro.property,
.token.punctuation,
.token.variable {
    color: #333
}

.token.builtin,
.token.important,
.token.keyword,
.token.operator,
.token.rule {
    color: #a71d5d
}

.token.attr-value,
.token.regex,
.token.string,
.token.url {
    color: #183691
}

.token.atrule,
.token.boolean,
.token.code,
.token.command,
.token.constant,
.token.entity,
.token.number,
.token.property,
.token.symbol {
    color: #0086b3
}

.token.prolog,
.token.selector,
.token.tag {
    color: #63a35c
}

.token.attr-name,
.token.class,
.token.class-name,
.token.function,
.token.id,
.token.namespace,
.token.pseudo-class,
.token.pseudo-element,
.token.url-reference .token.variable {
    color: #795da3
}

.token.entity {
    cursor: help
}

.token.title,
.token.title .token.punctuation {
    font-weight: 700;
    color: #1d3e81
}

.token.list {
    color: #ed6a43
}

.token.inserted {
    background-color: #eaffea;
    color: #55a532
}

.token.deleted {
    background-color: #ffecec;
    color: #bd2c00
}

.token.bold {
    font-weight: 700
}

.token.italic {
    font-style: italic
}

.language-json .token.property {
    color: #183691
}

.language-markup .token.tag .token.punctuation {
    color: #333
}

.language-css .token.function,
code.language-css {
    color: #0086b3
}

.language-yaml .token.atrule {
    color: #63a35c
}

code.language-yaml {
    color: #183691
}

.language-ruby .token.function {
    color: #333
}

.language-markdown .token.url {
    color: #795da3
}

.language-makefile .token.symbol {
    color: #795da3
}

.language-makefile .token.variable {
    color: #183691
}

.language-makefile .token.builtin {
    color: #0086b3
}

.language-bash .token.keyword {
    color: #0086b3
}

pre[data-line] {
    position: relative;
    padding: 1em 0 1em 3em
}

pre[data-line] .line-highlight-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    background-color: transparent;
    display: block;
    width: 100%
}

pre[data-line] .line-highlight {
    position: absolute;
    left: 0;
    right: 0;
    padding: inherit 0;
    margin-top: 1em;
    background: hsla(24, 20%, 50%, .08);
    background: linear-gradient(to right, hsla(24, 20%, 50%, .1) 70%, hsla(24, 20%, 50%, 0));
    pointer-events: none;
    line-height: inherit;
    white-space: pre
}

pre[data-line] .line-highlight:before,
pre[data-line] .line-highlight[data-end]:after {
    content: attr(data-start);
    position: absolute;
    top: .4em;
    left: .6em;
    min-width: 1em;
    padding: 0 .5em;
    background-color: hsla(24, 20%, 50%, .4);
    color: #f4f1ef;
    font: bold 65%/1.5 sans-serif;
    text-align: center;
    vertical-align: .3em;
    border-radius: 999px;
    text-shadow: none;
    box-shadow: 0 1px #fff
}

pre[data-line] .line-highlight[data-end]:after {
    content: attr(data-end);
    top: auto;
    bottom: .4em
}@font-face {
    font-family: Raleway;
    font-style: italic;
    font-weight: 400;
    src: url(https://fonts.gstatic.com/s/raleway/v34/1Pt_g8zYS_SKggPNyCgSQamb1W0lwk4S4WjMDrMfJQ.ttf) format('truetype')
}

@font-face {
    font-family: Raleway;
    font-style: italic;
    font-weight: 700;
    src: url(https://fonts.gstatic.com/s/raleway/v34/1Pt_g8zYS_SKggPNyCgSQamb1W0lwk4S4Y_LDrMfJQ.ttf) format('truetype')
}

@font-face {
    font-family: Raleway;
    font-style: normal;
    font-weight: 400;
    src: url(https://fonts.gstatic.com/s/raleway/v34/1Ptxg8zYS_SKggPN4iEgvnHyvveLxVvaorCIPrQ.ttf) format('truetype')
}

@font-face {
    font-family: Raleway;
    font-style: normal;
    font-weight: 700;
    src: url(https://fonts.gstatic.com/s/raleway/v34/1Ptxg8zYS_SKggPN4iEgvnHyvveLxVs9pbCIPrQ.ttf) format('truetype')
}

body {
    background: #fcfcfc;
    color: #111;
    font-family: Raleway, sans-serif;
    font-weight: 400;
    font-size: 16px
}

body .math,
body .mathjax-exps,
body h1,
body h2,
body h3,
body h4,
body h5,
body h6,
body li,
body table {
    color: #111;
    font-family: Raleway, sans-serif
}

body h1 {
    font-weight: 400;
    line-height: 4rem;
    margin: 0 0 1.75rem;
    padding: 20px 30px;
    text-align: center;
    text-transform: uppercase;
    margin-top: 4rem
}

body h2 {
    font-weight: 400;
    line-height: 3rem;
    margin: 0 0 1.9375rem;
    padding: 0 30px;
    text-align: center;
    text-transform: uppercase;
    margin-top: 3rem
}

body h3 {
    font-weight: 400
}

body h4 {
    font-weight: 400
}

body h5 {
    font-size: 1.125rem;
    font-weight: 400
}

body h6 {
    font-size: 1rem;
    font-weight: 700
}

body p {
    color: #111;
    font-size: 1rem;
    line-height: 1.75rem;
    margin: 0 0 1.25rem
}

body blockquote {
    margin-left: 1rem;
    margin-right: 0;
    padding: .5em
}

body h1 {
    font-size: 2.827rem
}

body h2 {
    font-size: 1.999rem
}

body h3 {
    font-size: 1.413rem
}

body h4 {
    font-size: 1.25rem
}

body h5 {
    font-size: 1.15rem
}

body h6 {
    font-size: 1rem
}

body a {
    color: #900;
    text-decoration: none
}

body a:hover {
    text-decoration: underline
}

body table {
    margin-bottom: 20px
}

body table td,
body table th {
    padding: 8px;
    line-height: 1.25rem;
    vertical-align: top;
    border-top: 1px solid #ddd
}

body table th {
    font-weight: 700
}

body table thead th {
    vertical-align: bottom
}

body table caption+thead tr:first-child td,
body table caption+thead tr:first-child th,
body table colgroup+thead tr:first-child td,
body table colgroup+thead tr:first-child th,
body table thead:first-child tr:first-child td,
body table thead:first-child tr:first-child th {
    border-top: 0
}

body table tbody+tbody {
    border-top: 2px solid #ddd
}

body code {
    padding: .5em;
    color: inherit;
    border: 1px solid #ccc;
    padding: .1em;
    font-size: .9em;
    margin-left: .2em;
    margin-right: .2em
}

body pre>code {
    border: none
}

body li>p {
    margin-top: 0;
    margin-bottom: 0
}

body .task-list {
    padding-left: 0
}

body .task-list-item {
    list-style: none;
    margin-left: -2.4em
}

body .task-list-item input {
    top: 3px;
    margin-right: 1.6em
}

body .task-list-item input:before {
    content: '';
    display: inline-block;
    width: 1rem;
    height: 1rem;
    vertical-align: middle;
    text-align: center;
    border: 1px solid gray;
    background-color: #fdfdfd;
    margin-left: 0;
    margin-top: -.8rem
}

body .task-list-item input:checked:before,
body .task-list-item input[checked]:before {
    content: '\25FC';
    font-size: .8125rem;
    line-height: .9375rem;
    margin-top: -1rem
}

@media screen and (-webkit-min-device-pixel-ratio:0) and (min-resolution:0.001dpcm) {
    body .task-list-item input:before {
        margin-top: -.2rem
    }

    body .task-list-item input:checked:before,
    body .task-list-item input[checked]:before {
        margin-top: -.2rem
    }
}

body blockquote {
    margin: 0 0 1.11111rem;
    padding: .5rem 1.11111rem 0 1.05556rem;
    border-left: 1px solid gray;
    background-color: #ededed
}

body blockquote,
body blockquote p {
    line-height: 1.6;
    color: #6f6f6f
}

body img {
    max-width: 100%
}

body kbd {
    color: #111
}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview ul{list-style:disc}.markdown-preview ul ul{list-style:circle}.markdown-preview ul ul ul{list-style:square}.markdown-preview ol{list-style:decimal}.markdown-preview ol ol,.markdown-preview ul ol{list-style-type:lower-roman}.markdown-preview ol ol ol,.markdown-preview ol ul ol,.markdown-preview ul ol ol,.markdown-preview ul ul ol{list-style-type:lower-alpha}.markdown-preview .newpage,.markdown-preview .pagebreak{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center!important}.markdown-preview:not([data-for=preview]) .code-chunk .code-chunk-btn-group{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .status{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .output-div{margin-bottom:16px}.markdown-preview .md-toc{padding:0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link div,.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}.markdown-preview .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0;min-height:100vh}@media screen and (min-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{font-size:14px!important;padding:1em}}@media print{html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc{padding:0 16px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link div,html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 300px / 2);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
.markdown-preview.markdown-preview {
  /*
   * ------------------------
   * scroll style
   * ------------------------
  */
  /*
   * -----------------------------------
   * h1 ~ h6
   * p, strong, dl, em, u, kbd, hr, mark
   * -----------------------------------
  */
  /*
   * ------------------------
   * image
   * ------------------------
  */
  /*
   * ------------------------
   * a, link
   * ------------------------
  */
  /*
   * ------------------------
   * ul, ol
   * ------------------------
  */
  /*
   * ------------------------
   * code
   * ------------------------
  */
  /*
   * ------------------------
   * pre
   * ------------------------
  */
  /*windows下code字体*/
  /*
   * ------------------------
   * blockquote
   * ------------------------
  */
  /*
   * ------------------------
   * sup, sub, kbd
   * ------------------------
  */
  /*
   * ------------------------
   * table
   * ------------------------
  */
  /*
   * ------------------------
   * btn
   * ------------------------
  */
  /*
   * ------------------------
   * select
   * ------------------------
   */
  /*
   * ------------------------
   * textarea
   * ------------------------
   */
  /*
   * ------------------------
   * input
   * ------------------------
  */
  /*input checkbox*/
  /*md-htmlblock*/
  /* #write */
  /*
   * ------------------------
   * typora-source
   * ------------------------
  */
  /*
   * ------------------------
   * #toc-dropmenu
   * ------------------------
  */
  /*
   * ------------------------
   * sidebar
   * toc-content
   * ------------------------
  */
  /*ty-on-outline-filter(catalog search style)*/
  /*
   * --------------------------
   * file-library-list
   * file-library-search-result
   * --------------------------
  */
  /*ty-search-item*/
  /*
   * ------------------------
   * file-library tree
   * ------------------------
  */
  /*
   * ------------------------
   * md-notification-container
   * ------------------------
  */
  /*
   * ------------------------
   * TOC md-toc
   * ------------------------
  */
  /*
   * ------------------------
   * code colors
   * ------------------------
  */
  /* apply to code fences with plan text*/
  /*
   * ------------------------
   * export html
   * ------------------------
  */
  /**
   * --------------------------------------
   * unibody-window
   * Control UI on Windows/Linux (optional)
   * --------------------------------------
  */
  /* .dropdown-menu */
  /* .megamenu-men */
  /* context menu */
  /*
   * ------------------------
   * UI-controls
   * dialogs
   * ------------------------
  */
  /* footer */
  /**
   * ---------------------------------
   * Control UI on Mac (optional)
   * ---------------------------------
  */
  /*
   * ------------------------
   * #root
   * .ty-preferences
   * ------------------------
  */
  /*
   * ------------------------
   * diagram
   * ------------------------
  */
  /*
   * ------------------------
   * @media print
   * ------------------------
  */
  /*
   * ------------------------
   * @media screen
   * ------------------------
  */
}
.markdown-preview.markdown-preview html {
  font-size: 16px;
  background-color: var(--bg-color);
  color: var(--text-color);
  letter-spacing: 0.5px;
  -webkit-font-smoothing: antialiased;
}
.markdown-preview.markdown-preview body {
  font-size: 16px;
  line-height: 1.6;
  box-sizing: border-box;
  overflow-x: hidden;
  font-family: "PingFang SC", "Microsoft YaHei", Arial, Helvetica, "Helvetica Neue", sans-serif !important;
  background: var(--bg-color);
}
.markdown-preview.markdown-preview body.os-windows {
  font-family: "Microsoft YaHei", Helvetica, "Helvetica Neue", Arial, sans-serif !important;
}
.markdown-preview.markdown-preview ::-webkit-scrollbar {
  width: 8px !important;
  height: 8px !important;
}
.markdown-preview.markdown-preview ::-webkit-scrollbar-thumb {
  background: var(--border-color) !important;
  border-radius: 4px !important;
}
.markdown-preview.markdown-preview ::-webkit-scrollbar-track {
  background: var(--bg-color2) !important;
}
.markdown-preview.markdown-preview body,
.markdown-preview.markdown-preview body div,
.markdown-preview.markdown-preview body pre,
.markdown-preview.markdown-preview body ul,
.markdown-preview.markdown-preview body section,
.markdown-preview.markdown-preview body content,
.markdown-preview.markdown-preview body #write {
  scrollbar-color: var(--border-color) var(--bg-color2) !important;
  scrollbar-width: thin !important;
}
.markdown-preview.markdown-preview body,
.markdown-preview.markdown-preview #megamenu-content,
.markdown-preview.markdown-preview header {
  background: var(--bg-color) !important;
}
.markdown-preview.markdown-preview header {
  box-shadow: 0 1px 3px 0 rgba(27, 27, 31, 0.16), 0 2px 5px 0 rgba(27, 27, 31, 0.1);
}
.markdown-preview.markdown-preview h1,
.markdown-preview.markdown-preview h2,
.markdown-preview.markdown-preview h3,
.markdown-preview.markdown-preview h4,
.markdown-preview.markdown-preview h5,
.markdown-preview.markdown-preview h6 {
  position: relative;
  line-height: 1.5;
  margin: 32px 0 16px;
  color: var(--title-color);
  letter-spacing: 2px;
}
.markdown-preview.markdown-preview h1 {
  font-size: 32px;
  border-bottom: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview h2 {
  font-size: 28px;
}
.markdown-preview.markdown-preview h3 {
  font-size: 24px;
}
.markdown-preview.markdown-preview h4 {
  font-size: 20px;
}
.markdown-preview.markdown-preview h5 {
  font-size: 18px;
}
.markdown-preview.markdown-preview h6 {
  font-size: 16px;
}
.markdown-preview.markdown-preview h2.md-focus:before,
.markdown-preview.markdown-preview h3.md-focus:before,
.markdown-preview.markdown-preview h4.md-focus:before,
.markdown-preview.markdown-preview h5.md-focus:before,
.markdown-preview.markdown-preview h6.md-focus:before,
.markdown-preview.markdown-preview h2:before,
.markdown-preview.markdown-preview h3:before,
.markdown-preview.markdown-preview h4:before,
.markdown-preview.markdown-preview h5:before,
.markdown-preview.markdown-preview h6:before {
  height: 20px;
  position: absolute;
  left: -36px;
  float: left;
  padding-left: 6px;
  padding-right: 3px;
  border: 1px solid;
  border-radius: 3px;
  font-size: 12px;
  font-weight: 400;
  color: var(--color-1);
  display: none;
}
.markdown-preview.markdown-preview h2:before,
.markdown-preview.markdown-preview h2.md-focus:before {
  content: 'h2';
  top: 11px;
}
.markdown-preview.markdown-preview h3:before,
.markdown-preview.markdown-preview h3.md-focus:before {
  content: 'h3';
  top: 8px;
}
.markdown-preview.markdown-preview h4:before,
.markdown-preview.markdown-preview h4.md-focus:before {
  content: 'h4';
  top: 5px;
}
.markdown-preview.markdown-preview h5:before,
.markdown-preview.markdown-preview h5.md-focus:before {
  content: 'h5';
  top: 4px;
}
.markdown-preview.markdown-preview h6:before,
.markdown-preview.markdown-preview h6.md-focus:before {
  content: 'h6';
  top: 3px;
}
.markdown-preview.markdown-preview h2:hover::before,
.markdown-preview.markdown-preview h3:hover::before,
.markdown-preview.markdown-preview h4:hover::before,
.markdown-preview.markdown-preview h5:hover::before,
.markdown-preview.markdown-preview h6:hover::before {
  display: block;
}
.markdown-preview.markdown-preview hr {
  border: none;
  height: 1px;
  margin: 16px 0;
  background: var(--border-color);
  box-sizing: border-box;
}
.markdown-preview.markdown-preview p {
  margin: 16px 0;
}
.markdown-preview.markdown-preview strong {
  font-weight: bold;
  color: var(--title-color);
}
.markdown-preview.markdown-preview u {
  text-decoration-color: var(--title-color);
  text-underline-offset: 4px;
}
.markdown-preview.markdown-preview em {
  font-weight: 400;
}
.markdown-preview.markdown-preview del {
  color: var(--text-grey);
  text-decoration: line-through;
  text-decoration-color: var(--color-5);
  -moz-text-decoration-color: var(--color-5);
}
.markdown-preview.markdown-preview mark {
  background-color: var(--color-1);
  color: var(--bg-color);
  padding: 0 4px;
  border-radius: 5px;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.markdown-preview.markdown-preview img {
  display: block;
  max-width: 100%;
  border-radius: 8px;
  margin: 0 auto 16px;
  color: var(--md-char-color);
}
.markdown-preview.markdown-preview span.md-image,
.markdown-preview.markdown-preview span.md-image span.md-content,
.markdown-preview.markdown-preview span.md-image span.md-image-src-span,
.markdown-preview.markdown-preview span.md-image span.md-image-before-src,
.markdown-preview.markdown-preview span.md-image span.md-image-after-src,
.markdown-preview.markdown-preview span.md-image span.md-image-input-src-btn,
.markdown-preview.markdown-preview span.md-image span.md-image-pick-file-btn,
.markdown-preview.markdown-preview span.md-image span.md-before::before,
.markdown-preview.markdown-preview span.md-image span.md-image-input-src-btn::before,
.markdown-preview.markdown-preview span.md-image span.md-image-pick-file-btn::before {
  color: var(--color-1);
}
.markdown-preview.markdown-preview span.md-image-btn {
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview span.md-image-btn:hover::before {
  color: var(--bg-color) !important;
}
.markdown-preview.markdown-preview span.md-image span.md-image-pick-file-btn {
  border-left-color: var(--border-color);
}
.markdown-preview.markdown-preview a {
  font-weight: 500;
  text-decoration: none;
  color: var(--color-1);
  font-size: 16px;
  text-underline-offset: 4px;
}
.markdown-preview.markdown-preview a:hover,
.markdown-preview.markdown-preview .md-link a:hover,
.markdown-preview.markdown-preview .footnotes a:hover {
  color: var(--color-1);
  text-decoration: underline;
  cursor: pointer !important;
}
.markdown-preview.markdown-preview .unibody-window a:hover,
.markdown-preview.markdown-preview content a:hover {
  color: var(--color-2) !important;
}
.markdown-preview.markdown-preview .footnotes,
.markdown-preview.markdown-preview .footnotes a {
  font-size: 14px;
  text-decoration: none;
}
.markdown-preview.markdown-preview .footnotes .md-def-name::before,
.markdown-preview.markdown-preview .footnotes .md-def-name::after {
  color: var(--text-color);
}
.markdown-preview.markdown-preview .footnotes .md-def-url,
.markdown-preview.markdown-preview .md-link .md-url {
  text-decoration: none;
  color: var(--color-1);
}
.markdown-preview.markdown-preview ul {
  margin-left: 0;
  margin-top: 5px;
  margin-bottom: 5px;
  padding-left: 36px;
}
.markdown-preview.markdown-preview ol {
  margin-left: 0;
  margin-top: 5px;
  margin-bottom: 5px;
  padding-left: 40px;
}
.markdown-preview.markdown-preview ul li,
.markdown-preview.markdown-preview ol li {
  margin-bottom: 5px;
}
.markdown-preview.markdown-preview ul > li > p {
  margin: 0 0 0 -2px;
}
.markdown-preview.markdown-preview ol > li > p {
  margin: 0 0 0 -6px;
}
.markdown-preview.markdown-preview ul > .task-list-item > input {
  margin-left: -22px;
}
.markdown-preview.markdown-preview ul ul {
  margin-left: -2px;
}
.markdown-preview.markdown-preview ol ol {
  margin-left: -7px;
}
.markdown-preview.markdown-preview ol > li > ul {
  margin-left: -7px;
}
.markdown-preview.markdown-preview ul > li > ol {
  margin-left: -2px;
}
.markdown-preview.markdown-preview code,
.markdown-preview.markdown-preview tt {
  margin: 16px 0;
  background: var(--bg-color2);
  font-size: 14px;
  box-sizing: border-box;
  border-radius: 5px;
  padding: 5px 10px;
  color: var(--text-code);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.markdown-preview.markdown-preview p code,
.markdown-preview.markdown-preview p tt,
.markdown-preview.markdown-preview li tt,
.markdown-preview.markdown-preview blockquote code,
.markdown-preview.markdown-preview blockquote tt,
.markdown-preview.markdown-preview .md-alert code,
.markdown-preview.markdown-preview .md-alert tt,
.markdown-preview.markdown-preview table code,
.markdown-preview.markdown-preview table tt {
  padding: 3px 6px !important;
  background: var(--color-1-0-a);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.markdown-preview.markdown-preview h1 code,
.markdown-preview.markdown-preview h2 code,
.markdown-preview.markdown-preview h3 code,
.markdown-preview.markdown-preview h4 code,
.markdown-preview.markdown-preview h5 code,
.markdown-preview.markdown-preview h6 code {
  vertical-align: middle;
}
.markdown-preview.markdown-preview h1 code {
  font-size: 18px;
}
.markdown-preview.markdown-preview h2 code {
  font-size: 16px;
}
.markdown-preview.markdown-preview .outline-content .outline-item code {
  font-size: 11px;
  padding: 2px 6px !important;
}
.markdown-preview.markdown-preview pre {
  margin: 16px 0;
  padding: 16px;
  background: var(--bg-color2) !important;
  font-size: 14px !important;
  box-sizing: border-box;
  border-radius: 8px;
  color: var(--text-code);
  overflow: auto;
}
.markdown-preview.markdown-preview pre pre {
  background: transparent !important;
  padding: 5px 10px;
  border: none;
}
.markdown-preview.markdown-preview pre .CodeMirror-sizer {
  padding-left: 10px;
}
.markdown-preview.markdown-preview .os-windows pre,
.markdown-preview.markdown-preview .os-windows code {
  font-family: initial !important;
}
.markdown-preview.markdown-preview blockquote,
.markdown-preview.markdown-preview .md-alert {
  margin: 16px 0;
  padding: 16px;
  background: var(--color-1-0-b);
  box-sizing: border-box;
  border-right: 10px solid transparent;
  border-left: 4px solid var(--color-1);
  border-radius: 8px;
}
.markdown-preview.markdown-preview blockquote blockquote {
  background: var(--color-1-0-b);
}
.markdown-preview.markdown-preview .md-alert.md-alert-note {
  border-left-color: var(--color-1);
  background: var(--color-1-0-b);
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview .md-alert.md-alert-note .md-alert-text-note {
  color: var(--color-1);
}
.markdown-preview.markdown-preview .md-alert.md-alert-caution {
  border-left-color: var(--color-2);
  background: var(--color-2-0-b);
  color: var(--color-2) !important;
}
.markdown-preview.markdown-preview .md-alert.md-alert-caution .md-alert-text-caution {
  color: var(--color-2);
}
.markdown-preview.markdown-preview .md-alert.md-alert-tip {
  border-left-color: var(--color-3);
  background: var(--color-3-0-b);
  color: var(--color-3) !important;
}
.markdown-preview.markdown-preview .md-alert.md-alert-tip .md-alert-text-tip {
  color: var(--color-3);
}
.markdown-preview.markdown-preview .md-alert.md-alert-important {
  border-left-color: var(--color-4);
  background: var(--color-4-0-b);
  color: var(--color-4) !important;
}
.markdown-preview.markdown-preview .md-alert.md-alert-important .md-alert-text-important {
  color: var(--color-4);
}
.markdown-preview.markdown-preview .md-alert.md-alert-warning {
  border-left-color: var(--color-5);
  background: var(--color-5-0-b);
  color: var(--color-5) !important;
}
.markdown-preview.markdown-preview .md-alert.md-alert-warning .md-alert-text-warning {
  color: var(--color-5);
}
.markdown-preview.markdown-preview sup,
.markdown-preview.markdown-preview sub {
  font-size: 12px;
}
.markdown-preview.markdown-preview sup.md-footnote {
  background: transparent;
  padding: 0 2px;
  color: var(--color-1);
}
.markdown-preview.markdown-preview sup.md-footnote:hover {
  color: var(--color-2);
  cursor: pointer;
}
.markdown-preview.markdown-preview kbd {
  display: inline-block;
  border: none;
  background: var(--title-color);
  color: var(--bg-color);
  border-image: none;
  border-radius: 5px;
  padding: 0 6px;
  font-size: 14px;
  box-shadow: none;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.markdown-preview.markdown-preview ::selection {
  background: var(--color-2-0-c);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.markdown-preview.markdown-preview figure.table-figure {
  margin-top: 24px !important;
}
.markdown-preview.markdown-preview table {
  width: 100%;
  margin-top: 16px;
  text-align: left;
  border-collapse: separate;
  border-spacing: 0;
  border-radius: 8px;
  overflow: hidden;
  padding: 0 !important;
  border: 1px solid var(--border-color) !important;
}
.markdown-preview.markdown-preview table thead tr th {
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview table tbody tr:nth-child(even) td {
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview table tbody tr:nth-child(odd) td {
  background: var(--bg-color);
}
.markdown-preview.markdown-preview table tr th,
.markdown-preview.markdown-preview table tr td {
  padding: 10px !important;
}
.markdown-preview.markdown-preview table tr td,
.markdown-preview.markdown-preview table thead tr th {
  border-left: 1px solid var(--border-color) !important;
}
.markdown-preview.markdown-preview table tbody tr td:first-of-type,
.markdown-preview.markdown-preview table thead tr th:first-of-type {
  border-left: none !important;
}
.markdown-preview.markdown-preview .md-grid-board-wrap table {
  border: none !important;
  border-radius: 0;
}
.markdown-preview.markdown-preview .md-grid-board-wrap table tr th,
.markdown-preview.markdown-preview .md-grid-board-wrap table tr td {
  padding: 0 !important;
}
.markdown-preview.markdown-preview #md-grid-width,
.markdown-preview.markdown-preview #md-grid-height {
  text-align: center !important;
  padding: 1px 0;
}
.markdown-preview.markdown-preview button#md-resize-grid {
  display: block !important;
  width: 100%;
  margin: 5px auto 0;
  color: var(--bg-color);
}
.markdown-preview.markdown-preview button#md-resize-grid:hover {
  color: var(--color-1);
  border-color: var(--color-1);
}
.markdown-preview.markdown-preview .btn,
.markdown-preview.markdown-preview .btn-default,
.markdown-preview.markdown-preview .long-btn {
  border-radius: 5px !important;
  border: 1px solid var(--border-color) !important;
  background: var(--bg-color2) !important;
  outline: none !important;
}
.markdown-preview.markdown-preview .btn-primary {
  background: var(--color-1) !important;
  border-color: var(--color-1) !important;
  color: #fff;
}
.markdown-preview.markdown-preview .btn:hover,
.markdown-preview.markdown-preview .long-btn:hover {
  background: var(--color-1-0-b) !important;
  color: var(--color-1);
  border-color: var(--color-1) !important;
}
.markdown-preview.markdown-preview .btn:focus,
.markdown-preview.markdown-preview .long-btn:focus {
  outline: none !important;
}
.markdown-preview.markdown-preview .btn {
  margin: 2px;
  margin-right: 5px !important;
}
.markdown-preview.markdown-preview .btn:last-of-type {
  margin-right: 0;
}
.markdown-preview.markdown-preview .dropdown-toggle::after {
  font-size: 14px;
}
.markdown-preview.markdown-preview .dropdown-toggle:hover::after {
  color: var(--color-1);
}
.markdown-preview.markdown-preview select {
  border-radius: 5px !important;
  border: 1px solid var(--border-color) !important;
  padding: 6px !important;
}
.markdown-preview.markdown-preview option {
  background: var(--bg-color);
}
.markdown-preview.markdown-preview textarea {
  outline: none;
  padding: 5px;
  border-radius: 5px;
  border-color: var(--border-color);
  background: var(--bg-color) !important;
  color: var(--text-color) !important;
  font-family: "PingFang SC", "Helvetica Neue", Helvetica, "Microsoft YaHei", Arial, sans-serif !important;
}
.markdown-preview.markdown-preview html input,
.markdown-preview.markdown-preview input {
  border-radius: 5px !important;
  line-height: 1.2;
  position: relative;
  padding: 0 6px;
  outline: none !important;
  border: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview html input:focus,
.markdown-preview.markdown-preview html textarea:focus,
.markdown-preview.markdown-preview html input[type="number"]:focus,
.markdown-preview.markdown-preview html input[type="search"]:focus,
.markdown-preview.markdown-preview html input[type="text"]:focus {
  border-color: var(--color-1) !important;
  box-shadow: none !important;
  outline: none !important;
}
.markdown-preview.markdown-preview input::placeholder {
  font-size: 14px;
  color: var(--text-grey) !important;
}
.markdown-preview.markdown-preview input[type="checkbox"],
.markdown-preview.markdown-preview input[type="radio"] {
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  width: 14px !important;
  height: 14px !important;
  padding: 0 !important;
  box-sizing: border-box;
  border: 1px solid var(--border-color) !important;
  border-radius: 3px !important;
  list-style: none;
  background: transparent !important;
  margin-right: 10px !important;
  margin-bottom: -2px !important;
}
.markdown-preview.markdown-preview input[type="checkbox"]:checked,
.markdown-preview.markdown-preview input[type="radio"]:checked {
  background: var(--color-1) !important;
  border-color: transparent !important;
}
.markdown-preview.markdown-preview input[type="checkbox"]:checked::after {
  content: "";
  display: block;
  width: 5px;
  height: 9px;
  background: transparent;
  border: 2px solid #fff;
  border-top: none;
  border-left: none;
  position: absolute;
  box-sizing: border-box;
  bottom: 2.5px;
  left: 3.5px;
  transform: rotate(40deg);
  -webkit-transform: rotate(40deg);
  -ms-transform: rotate(40deg);
}
.markdown-preview.markdown-preview input[type="radio"] {
  border-radius: 50% !important;
  -webkit-border-radius: 50% !important;
  -moz-border-radius: 50% !important;
  border: 1px solid var(--border-color) !important;
}
.markdown-preview.markdown-preview input[type="radio"]:checked::after {
  content: "";
  display: block;
  width: 5px;
  height: 5px;
  box-sizing: border-box;
  margin: 3.5px auto;
  background: #fff;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
}
.markdown-preview.markdown-preview span.md-comment {
  color: var(--md-char-color);
  opacity: 1;
}
.markdown-preview.markdown-preview .md-image-btn.selected,
.markdown-preview.markdown-preview .md-image-btn:hover {
  background: var(--color-1);
}
.markdown-preview.markdown-preview #write pre.md-meta-block:empty:before {
  color: var(--text-code);
}
.markdown-preview.markdown-preview .md-htmlblock-panel,
.markdown-preview.markdown-preview .md-htmlblock-container,
.markdown-preview.markdown-preview .md-htmlblock-container .md-htmlblock-panel-placeholder {
  background: transparent;
}
.markdown-preview.markdown-preview .md-htmlblock-container,
.markdown-preview.markdown-preview .md-math-container {
  border-radius: 8px;
  background: var(--bg-color);
}
.markdown-preview.markdown-preview .md-math-container {
  width: 100%;
  padding: 5px 10px;
}
.markdown-preview.markdown-preview .md-rawblock-on-edit .md-htmlblock-panel,
.markdown-preview.markdown-preview .md-rawblock-on-edit .md-mathblock-panel {
  border-radius: 8px;
  background: var(--bg-color2);
  padding: 5px 10px;
}
.markdown-preview.markdown-preview .md-mathblock-panel .md-rawblock-control {
  background: transparent;
}
.markdown-preview.markdown-preview .md-mathblock-panel .code-tooltip {
  box-shadow: none;
  margin: 16px 6px 0;
  border-top: 1px solid var(--border-color);
  border-radius: 0;
}
.markdown-preview.markdown-preview .md-rawblock-on-edit .md-rawblock-input {
  padding: 0;
  background: transparent;
}
.markdown-preview.markdown-preview .md-rawblock .md-rawblock-tooltip {
  height: auto;
  margin-top: -10px;
  float: right;
  border-radius: 5px;
  background: var(--bg-color2);
  animation: none !important;
}
.markdown-preview.markdown-preview .md-rawblock .md-rawblock-tooltip span {
  padding-top: 4px;
  padding-bottom: 4px;
}
.markdown-preview.markdown-preview .md-rawblock .md-rawblock-tooltip-edit-btn:hover {
  background: transparent;
}
.markdown-preview.markdown-preview .md-htmlblock:hover .md-htmlblock-container,
.markdown-preview.markdown-preview .md-htmlblock:hover .md-rawblock-tooltip,
.markdown-preview.markdown-preview .md-math-block:hover .md-math-container,
.markdown-preview.markdown-preview .md-math-block:hover .md-rawblock-tooltip,
.markdown-preview.markdown-preview .md-rawblock-on-edit:hover .md-rawblock-tooltip {
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview .md-inline-math script {
  color: #f9007c;
}
.markdown-preview.markdown-preview #write {
  width: 100%;
  margin: 0 auto;
  padding: 0 96px 32px;
  scroll-behavior: smooth;
  scroll-padding: 10px;
  overflow-x: scroll;
}
.markdown-preview.markdown-preview #typora-sidebar {
  border-right-color: var(--border-color);
}
.markdown-preview.markdown-preview #typora-source .CodeMirror-sizer {
  padding-right: 0 !important;
}
.markdown-preview.markdown-preview #typora-source .CodeMirror-lines {
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
  padding: 32px 100px;
  box-sizing: border-box;
}
.markdown-preview.markdown-preview #typora-source .CodeMirror-lines pre {
  font-size: 16px !important;
  border: none !important;
  padding: 8px 16px !important;
  background: var(--bg-color) !important;
}
.markdown-preview.markdown-preview #typora-source .CodeMirror-activeline pre {
  background: var(--bg-color2) !important;
  border-radius: 6px;
}
.markdown-preview.markdown-preview #typora-source .CodeMirror-activeline .CodeMirror-linebackground {
  background: transparent !important;
}
.markdown-preview.markdown-preview #typora-source .CodeMirror-lines .cm-header {
  color: var(--color-2);
}
.markdown-preview.markdown-preview .CodeMirror.cm-s-typora-default div.CodeMirror-cursor {
  border-left: 3px solid var(--color-2);
}
.markdown-preview.markdown-preview .cm-s-typora-default .cm-header,
.markdown-preview.markdown-preview .cm-s-typora-default .cm-property,
.markdown-preview.markdown-preview .cm-s-typora-default .cm-link {
  color: var(--color-2);
}
.markdown-preview.markdown-preview .cm-s-inner .cm-comment,
.markdown-preview.markdown-preview .cm-s-inner.cm-comment,
.markdown-preview.markdown-preview .cm-overlay {
  color: var(--md-char-color);
}
.markdown-preview.markdown-preview .cm-s-typora-default .cm-string {
  color: var(--color-1);
}
.markdown-preview.markdown-preview .cm-s-typora-default .cm-code,
.markdown-preview.markdown-preview .cm-s-typora-default .cm-comment {
  color: var(--text-code);
}
.markdown-preview.markdown-preview .cm-s-typora-default .cm-tag {
  color: #e31570;
}
.markdown-preview.markdown-preview .cm-attribute {
  color: var(--color-2);
}
.markdown-preview.markdown-preview #toc-dropmenu {
  border-top-left-radius: 8px;
  border-bottom-left-radius: 8px;
  top: calc(var(--title-bar-height) - 2px) !important;
  right: 18px;
}
.markdown-preview.markdown-preview #toc-dropmenu #pin-outline-btn {
  display: inline-block;
  top: 10px;
}
.markdown-preview.markdown-preview #toc-dropmenu .divider {
  margin-bottom: 0;
}
.markdown-preview.markdown-preview .outline-content {
  padding: 16px;
  font-size: 14px !important;
}
.markdown-preview.markdown-preview .outline-content li {
  position: relative;
  z-index: 30;
  line-height: 1.3 !important;
  margin-bottom: 0;
}
.markdown-preview.markdown-preview .outline-content li::before {
  content: '';
  width: 1px;
  height: calc(100% + 5px);
  position: absolute;
  z-index: 30;
  left: 0;
  top: 0;
  background: var(--border-color);
}
.markdown-preview.markdown-preview .outline-content > li:first-of-type::before {
  top: 0;
}
.markdown-preview.markdown-preview .outline-content li a {
  font-size: 14px !important;
}
.markdown-preview.markdown-preview .outline-content li ul {
  margin-left: 19px;
  position: relative;
  z-index: 48;
}
.markdown-preview.markdown-preview .outline-content ul li::before {
  content: '';
  width: 1px;
  height: calc(100% + 11px);
  top: -11px;
}
.markdown-preview.markdown-preview .outline-content ul > li:last-of-type::before {
  content: '';
  width: 1px;
  height: 11px;
  top: -11px;
}
.markdown-preview.markdown-preview .outline-content li .outline-item {
  margin: 0 0 3px 6px;
  padding: 4px 8px;
  position: relative;
  z-index: 50;
  border-radius: 5px;
  border: 1px solid transparent;
}
.markdown-preview.markdown-preview .outline-content li .outline-item:hover,
.markdown-preview.markdown-preview .outline-content li .outline-item-active {
  z-index: 70;
}
.markdown-preview.markdown-preview .outline-content .outline-label {
  margin: 0;
  padding: 0;
  position: relative;
  z-index: 70;
  background: var(--bg-color);
  text-decoration: none;
}
.markdown-preview.markdown-preview .outline-content li .outline-item::before {
  content: '';
  width: 12px;
  height: calc(50% + 12px);
  position: absolute;
  z-index: 38;
  left: -7px;
  top: -12px;
  background: transparent;
  border-left: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview .outline-content > li:first-of-type > .outline-item::before {
  top: calc(50% - 1px) !important;
  border-top: 1px solid var(--border-color);
  border-bottom: none;
  border-top-left-radius: 3px;
  -webkit-border-top-left-radius: 3px;
  border-bottom-left-radius: 0;
  -webkit-border-bottom-left-radius: 0;
}
.markdown-preview.markdown-preview .outline-content > li:first-of-type > .outline-item::after {
  content: '';
  width: 1px;
  height: 100%;
  position: absolute;
  z-index: 35;
  left: -7px;
  top: -2px;
  background: var(--bg-color);
}
.markdown-preview.markdown-preview .outline-content > li:last-of-type > .outline-item::before,
.markdown-preview.markdown-preview .outline-content > li ul > li:last-of-type > .outline-item::before {
  z-index: 90;
  background: transparent;
  border-left: 1px solid var(--border-color);
  border-top: 1px solid var(--border-color);
  border-top: none !important;
  border-bottom-left-radius: 3px;
  -webkit-border-bottom-left-radius: 3px;
  border-top-left-radius: 0;
  -webkit-border-top-left-radius: 0;
}
.markdown-preview.markdown-preview .outline-content > li:only-of-type > .outline-item::before,
.markdown-preview.markdown-preview .outline-content > li:last-of-type::before {
  display: none;
}
.markdown-preview.markdown-preview .outline-content > li:only-of-type {
  margin-left: -6px;
}
.markdown-preview.markdown-preview .outline-expander {
  padding-left: 0;
  width: auto;
  height: 8px;
  background: transparent;
}
.markdown-preview.markdown-preview .outline-expander:before {
  font-size: 10px;
  padding: 0 3px 0 2px;
  margin-top: -2px;
  margin-left: -2px;
  background: transparent;
}
.markdown-preview.markdown-preview .outline-item:hover,
.markdown-preview.markdown-preview .outline-item:hover > .outline-label,
.markdown-preview.markdown-preview .outline-item:hover > .outline-expander:before,
.markdown-preview.markdown-preview .outline-item:hover > .outline-expander,
.markdown-preview.markdown-preview .outline-item-active,
.markdown-preview.markdown-preview .outline-item-active > span,
.markdown-preview.markdown-preview .outline-item-active > .outline-expander:before {
  background: var(--bg-color2) !important;
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview .outline-item:hover {
  z-index: 70;
}
.markdown-preview.markdown-preview .outline-item:hover::before,
.markdown-preview.markdown-preview .outline-item-active::before {
  width: 6px !important;
}
.markdown-preview.markdown-preview .outline-item-active .outline-expander {
  font-weight: bold !important;
}
.markdown-preview.markdown-preview .ty-on-outline-filter .outline-content li::before,
.markdown-preview.markdown-preview .ty-on-outline-filter .outline-content .outline-item::before,
.markdown-preview.markdown-preview .ty-on-outline-filter .outline-content .outline-item::after {
  display: none;
}
.markdown-preview.markdown-preview .ty-on-outline-filter .outline-content > li > ul,
.markdown-preview.markdown-preview .ty-on-outline-filter .outline-content .outline-item {
  margin-left: 0;
}
.markdown-preview.markdown-preview #file-library-list,
.markdown-preview.markdown-preview #file-library-search-result {
  padding: 12px;
}
.markdown-preview.markdown-preview #file-library-list .file-list-item,
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item {
  margin-bottom: 8px;
  border: 1px solid transparent;
  border-bottom-color: var(--border-color);
}
.markdown-preview.markdown-preview #file-library-list .file-list-item {
  padding-left: 12px;
  padding-right: 12px;
}
.markdown-preview.markdown-preview #file-library-list .file-list-item.active,
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item.active {
  border-radius: 5px;
  border: 1px solid var(--color-1);
}
.markdown-preview.markdown-preview #file-library-list .file-list-item.active {
  padding: 6px 12px;
}
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item.active {
  padding: 6px 12px 6px 0;
}
.markdown-preview.markdown-preview #file-library-list .file-list-item:first-of-type.active,
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item:first-of-type.active {
  margin-top: 0;
}
.markdown-preview.markdown-preview #file-library-list .file-list-item:hover,
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item:hover {
  border-radius: 5px;
  background: var(--bg-color2);
  border-color: var(--border-color);
}
.markdown-preview.markdown-preview #file-library-list .file-list-item:hover {
  color: var(--color-1);
}
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item.active {
  color: var(--text-color);
}
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item.active .ty-search-item-line {
  white-space: normal;
  word-break: break-word;
}
.markdown-preview.markdown-preview .file-list-item-time {
  margin-right: 0;
  padding-right: 0;
}
.markdown-preview.markdown-preview .file-list-item-count {
  margin-right: 0;
  padding: 0 6px;
  border-radius: 3px;
}
.markdown-preview.markdown-preview #file-library-search-result .ty-search-item.active .file-list-item-count {
  padding-right: 0;
}
.markdown-preview.markdown-preview #sidebar-loading-template {
  padding: 8px;
  border-radius: 5px;
  margin-bottom: 5px;
  display: none;
}
.markdown-preview.markdown-preview .ty-file-search-match-text {
  background-color: var(--color-2-0-c);
}
.markdown-preview.markdown-preview #file-library-tree {
  padding-top: 8px;
  padding-left: 12px;
  padding-right: 12px;
  box-sizing: border-box;
}
.markdown-preview.markdown-preview .sidebar-tab-btn {
  font-size: 16px !important;
  line-height: 20px !important;
  vertical-align: text-top;
  margin-top: 12px !important;
  color: var(--title-color);
}
.markdown-preview.markdown-preview .sidebar-tab-btn:hover {
  color: var(--color-1);
}
.markdown-preview.markdown-preview .ty-sidebar-search-panel .searchpanel-search-option-btn {
  top: 8px;
}
.markdown-preview.markdown-preview .file-tree-node {
  position: relative;
  padding-left: 0;
  margin-left: 0;
}
.markdown-preview.markdown-preview .file-library-node .file-node-children {
  margin-left: 12px;
}
.markdown-preview.markdown-preview .file-node-expanded > .file-node-children {
  margin-left: 28px;
}
.markdown-preview.markdown-preview .file-tree-node.active > .file-node-background {
  border-radius: 5px;
  border: none;
}
.markdown-preview.markdown-preview .fa-folder:before,
.markdown-preview.markdown-preview .fa-folder:after,
.markdown-preview.markdown-preview .fa-caret-right,
.markdown-preview.markdown-preview .fa-caret-down {
  color: var(--text-grey);
}
.markdown-preview.markdown-preview .ty-on-outline-filter .ty-outline-hit {
  color: var(--color-1);
}
.markdown-preview.markdown-preview #md-notification {
  position: relative;
}
.markdown-preview.markdown-preview #md-notification::before {
  font-size: 18px;
  top: 13px;
}
.markdown-preview.markdown-preview #md-notification p {
  margin: auto;
  font-size: 14px;
}
.markdown-preview.markdown-preview #md-notification > p {
  margin-top: 10px;
}
.markdown-preview.markdown-preview #md-notification-content span {
  margin-bottom: 4px;
  margin-left: 8px;
}
.markdown-preview.markdown-preview .typora-search-spinner > div {
  background: var(--color-1);
}
.markdown-preview.markdown-preview .md-toc {
  font-size: 14px;
  margin: 20px 0 0 0;
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview .md-toc-content {
  padding: 20px 30px;
  margin: 16px 0;
}
.markdown-preview.markdown-preview .md-toc-content .md-toc-item {
  margin-bottom: 6px;
}
.markdown-preview.markdown-preview #write div.md-toc-tooltip {
  position: relative;
  margin: 12px 0 6px;
}
.markdown-preview.markdown-preview #write div.md-toc-tooltip .md-delete-toc {
  padding: 0 12px;
  background: var(--bg-color);
}
.markdown-preview.markdown-preview #write div.md-toc-tooltip .md-delete-toc:hover {
  color: var(--color-1);
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview .cm-s-inner {
  background-color: transparent !important;
  color: var(--text-code);
}
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-gutters {
  background: transparent !important;
  color: var(--text-code);
  border: none;
}
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-guttermarker,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-guttermarker-subtle,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-linenumber {
  color: var(--md-char-color);
}
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-cursor {
  border-left: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview .cm-s-inner div.CodeMirror-selected,
.markdown-preview.markdown-preview .cm-s-inner.CodeMirror-focused div.CodeMirror-selected,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-line::selection,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-line > span::selection,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-line > span > span::selection,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-line::-moz-selection,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-line > span::-moz-selection,
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-line > span > span::-moz-selection {
  background: var(--color-2-0-c);
}
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-activeline-background {
  background: transparent;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-keyword {
  color: #bb59fd;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-operator {
  color: #6ac0df;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-variable {
  color: #f4395dff;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-variable-2 {
  color: #e2785f;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-variable-3 {
  color: #0d50cc;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-builtin {
  color: #f61d78;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-atom {
  color: #fa5336;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-number {
  color: #f59102;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-def {
  color: #4087fd;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-string {
  color: #00a866;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-string-2 {
  color: #01b001;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-comment,
.markdown-preview.markdown-preview .cm-s-inner .cm-meta {
  color: var(--md-char-color);
}
.markdown-preview.markdown-preview .cm-s-inner .cm-attribute {
  color: #c08b01;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-property {
  color: #1b9f72;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-qualifier {
  color: #dc7b45;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-tag {
  color: #e32e73;
}
.markdown-preview.markdown-preview .cm-s-inner .cm-tag.cm-bracket {
  color: #01a2da;
}
.markdown-preview.markdown-preview .cm-s-inner .CodeMirror-matchingbracket {
  text-decoration: underline;
  color: var(--text-code) !important;
}
.markdown-preview.markdown-preview .md-fences {
  background-color: var(--bg-color2);
  color: var(--text-grey);
}
.markdown-preview.markdown-preview .md-fences .code-tooltip {
  background-color: var(--bg-color2);
  color: var(--text-color);
  border-radius: 5px;
  z-index: 50;
  padding: 0;
  bottom: -2.9em;
}
.markdown-preview.markdown-preview .md-fences .code-tooltip input,
.markdown-preview.markdown-preview .md-fences .code-tooltip span {
  padding: 3px;
  border-radius: 5px;
}
.markdown-preview.markdown-preview body.typora-export {
  padding-left: 0;
  padding-right: 0;
}
.markdown-preview.markdown-preview .typora-export-content,
.markdown-preview.markdown-preview .typora-export-show-outline .typora-export-content {
  width: 100vw;
  max-width: 2560px;
  height: 100vh;
  margin: 0 auto;
}
.markdown-preview.markdown-preview .typora-export-sidebar {
  margin-right: 0;
  margin-top: 0;
  border-right: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content {
  padding-top: 18px;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content li ul {
  margin-left: 20px;
  z-index: 55;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content .outline-item-active > .outline-item {
  position: relative;
  z-index: 70;
}
.markdown-preview.markdown-preview .typora-export .outline-content li a {
  font-weight: normal;
  padding: 2px 0;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content li,
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content li > .outline-item {
  background: var(--bg-color) !important;
  color: var(--text-color) !important;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content ul li::before,
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content ul > li:last-of-type::before {
  top: 0;
}
.markdown-preview.markdown-preview .typora-export .outline-content li .outline-item::before {
  height: calc(50% + 13px);
  top: -13px;
}
.markdown-preview.markdown-preview .typora-export-no-collapse-outline .outline-content li .outline-item::before {
  height: calc(50% + 10px);
  top: -10px;
}
.markdown-preview.markdown-preview .typora-export .outline-expander::before {
  content: "";
  width: 7px;
  height: 7px;
  background: transparent;
  box-sizing: border-box;
  border-top: 1px solid var(--text-color);
  border-right: 1px solid var(--text-color);
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  margin-right: 5px;
  margin-top: 1px;
}
.markdown-preview.markdown-preview .typora-export .outline-expander:hover::before {
  content: "";
  width: 7px;
  height: 7px;
  background: transparent;
  box-sizing: border-box;
  border-top: 1px solid var(--text-color);
  border-right: 1px solid var(--text-color);
  transform: rotate(135deg);
  -webkit-transform: rotate(135deg);
  -ms-transform: rotate(135deg);
  margin-right: 5px;
  margin-left: 2px;
  margin-top: -2px;
}
.markdown-preview.markdown-preview .typora-export .outline-item-open > .outline-item > .outline-expander::before {
  content: "";
  width: 7px;
  height: 7px;
  background: transparent;
  box-sizing: border-box;
  border-top: 1px solid var(--text-color);
  border-right: 1px solid var(--text-color);
  transform: rotate(135deg);
  -webkit-transform: rotate(135deg);
  -ms-transform: rotate(135deg);
  margin-right: 5px;
  margin-left: 2px;
  margin-top: -2px;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content li .outline-item:hover,
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content .outline-item-active > .outline-item,
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content .outline-item-active > .outline-item > .outline-label {
  background: var(--bg-color2) !important;
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content li > .outline-item:hover > .outline-expander::before {
  border-color: var(--color-1);
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-item-active > .outline-item::before {
  width: 6px;
}
.markdown-preview.markdown-preview .typora-export .outline-item:hover {
  margin-right: 0;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-item-active::before {
  width: 1px !important;
}
.markdown-preview.markdown-preview .typora-export-sidebar .outline-content .outline-item-active > .outline-item > .outline-expander::before {
  border-width: 2px !important;
  border-color: var(--color-1);
}
.markdown-preview.markdown-preview .typora-export-no-collapse-outline .outline-expander {
  display: none;
}
.markdown-preview.markdown-preview body.unibody-window,
.markdown-preview.markdown-preview body.typora-export {
  letter-spacing: 1px;
}
.markdown-preview.markdown-preview .unibody-window .outline-content li .outline-item::before {
  height: calc(50% + 13px);
  top: -13px;
}
.markdown-preview.markdown-preview .unibody-window #write ul,
.markdown-preview.markdown-preview .typora-export #write ul {
  padding-left: 34px;
}
.markdown-preview.markdown-preview .unibody-window #write ol,
.markdown-preview.markdown-preview .typora-export #write ol {
  padding-left: 36px;
}
.markdown-preview.markdown-preview .unibody-window #write ul ul,
.markdown-preview.markdown-preview .unibody-window #write ol ol,
.markdown-preview.markdown-preview .unibody-window #write ul > li > ol,
.markdown-preview.markdown-preview .unibody-window #write ol > li > ul,
.markdown-preview.markdown-preview .typora-export #write ul ul,
.markdown-preview.markdown-preview .typora-export #write ol ol,
.markdown-preview.markdown-preview .typora-export #write ul > li > ol,
.markdown-preview.markdown-preview .typora-export #write ol > li > ul {
  margin-left: 0;
}
.markdown-preview.markdown-preview .unibody-window #write ul > li > p,
.markdown-preview.markdown-preview .typora-export #write ul > li > p {
  margin: 0;
}
.markdown-preview.markdown-preview .unibody-window #write ol > li > p,
.markdown-preview.markdown-preview .typora-export #write ol > li > p {
  margin: 0 0 0 -2px;
}
.markdown-preview.markdown-preview .unibody-window #write ul > .task-list-item > input,
.markdown-preview.markdown-preview .typora-export #write ul > .task-list-item > input {
  margin-left: -22px;
}
.markdown-preview.markdown-preview .unibody-window #megamenu-menu-sidebar {
  --bg-color: #1b1b1f;
  --bg-color2: #282a32;
  --border-color: #3c3e46;
  overflow: hidden;
}
.markdown-preview.markdown-preview #megamenu-menu-sidebar .megamenu-menu-list {
  border: none;
}
.markdown-preview.markdown-preview #recent-file-panel-search-input,
.markdown-preview.markdown-preview .megamenu-menu-panel .btn,
.markdown-preview.markdown-preview .dropdown-menu li a {
  border-radius: 5px !important;
}
.markdown-preview.markdown-preview .unibody-window .long-btn {
  border-radius: 8px !important;
}
.markdown-preview.markdown-preview .dropdown-menu {
  padding: 6px !important;
  border-radius: 8px !important;
  border: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview .dropdown-menu li {
  border-radius: 5px !important;
}
.markdown-preview.markdown-preview .ty-spell-check-panel-item {
  border-radius: 5px;
  margin-bottom: 4px;
}
.markdown-preview.markdown-preview .btn-split-group .dropdown-menu {
  margin-top: 0;
}
.markdown-preview.markdown-preview .input-group-content .dropdown-menu li:hover {
  border-radius: 5px;
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview .dropdown-menu li:hover a {
  color: var(--color-1);
}
.markdown-preview.markdown-preview .dropdown-menu li a {
  font-size: 14px;
}
.markdown-preview.markdown-preview .dropdown-menu table th,
.markdown-preview.markdown-preview .dropdown-menu table td {
  padding: 5px !important;
}
.markdown-preview.markdown-preview #sidebar-files-menu li:hover a {
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview #close-sidebar-menu-btn:hover {
  color: var(--color-1);
}
.markdown-preview.markdown-preview .unibody-window .long-btn:hover span,
.markdown-preview.markdown-preview .unibody-window .long-btn:hover i,
.markdown-preview.markdown-preview .megamenu-menu-panel .btn:hover,
.markdown-preview.markdown-preview #megamenu-menu-header-title:hover,
.markdown-preview.markdown-preview .toolbar-icon:hover,
.markdown-preview.markdown-preview #megamenu-back-btn:hover i {
  color: var(--color-1) !important;
  text-decoration: none;
}
.markdown-preview.markdown-preview .unibody-window .long-btn:hover,
.markdown-preview.markdown-preview .megamenu-menu-panel .btn:hover,
.markdown-preview.markdown-preview #megamenu-back-btn:hover {
  border-color: var(--color-1);
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview #w-traffic-lights span {
  background: transparent !important;
  border-radius: 0 !important;
  border: none !important;
}
.markdown-preview.markdown-preview #w-traffic-lights span:hover {
  background: var(--color-1-0-b) !important;
}
.markdown-preview.markdown-preview #w-traffic-lights #w-close:hover {
  background: var(--color-5) !important;
  color: #fff !important;
}
.markdown-preview.markdown-preview #top-titlebar .btn {
  margin: 0 !important;
}
.markdown-preview.markdown-preview .megamenu-menu-panel .btn {
  padding: 6px 12px;
}
.markdown-preview.markdown-preview .megamenu-content {
  padding-right: 30px;
}
.markdown-preview.markdown-preview #open-theme-folder-btn {
  margin-top: 48px;
}
.markdown-preview.markdown-preview .megamenu-menu {
  background: var(--bg-color);
}
.markdown-preview.markdown-preview .megamenu-menu-header {
  border-color: var(--bg-color2);
}
.markdown-preview.markdown-preview #megamenu-back-btn {
  border-radius: 5px;
  border-color: var(--border-color);
  margin-left: 10px;
}
.markdown-preview.markdown-preview #megamenu-menu-list {
  padding: 0 8px;
}
.markdown-preview.markdown-preview .megamenu-menu-list li {
  margin-top: 6px;
}
.markdown-preview.markdown-preview .megamenu-menu-list li a {
  font-size: 16px;
  border-radius: 5px;
  display: block;
  border: 1px solid transparent;
  line-height: 34px;
}
.markdown-preview.markdown-preview .megamenu-menu-list li a .fa {
  font-size: 18px;
  line-height: 38px;
  display: inline-block;
  margin-left: 0;
  opacity: 1;
}
.markdown-preview.markdown-preview .megamenu-menu-list li #m-saved .fa {
  font-size: 20px;
  line-height: 36px !important;
  color: var(--color-2);
}
.markdown-preview.markdown-preview .megamenu-menu-list li a:hover,
.markdown-preview.markdown-preview .megamenu-menu-list li a.active:hover {
  color: var(--color-1) !important;
  background: var(--bg-color2) !important;
  border: 1px solid var(--color-1);
}
.markdown-preview.markdown-preview .megamenu-menu-list li a:hover,
.markdown-preview.markdown-preview .megamenu-menu-list li a.active {
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview .megamenu-menu-list li a.active {
  color: var(--color-1) !important;
}
@media (max-width: 530px) {
  .markdown-preview.markdown-preview #megamenu-back-btn {
    margin-left: 0;
  }
  .markdown-preview.markdown-preview .megamenu-menu-list li a i {
    margin-left: 10px;
  }
}
.markdown-preview.markdown-preview #recent-file-panel-action-btn-container {
  margin-right: 0;
}
.markdown-preview.markdown-preview #recent-file-panel-search-input {
  width: calc(100% - 55px);
}
.markdown-preview.markdown-preview #megamenu-clear-recet-documents {
  background: var(--bg-color2);
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 14px;
}
.markdown-preview.markdown-preview #megamenu-clear-recet-documents:hover {
  color: var(--color-1);
}
.markdown-preview.markdown-preview .ty-show-search #info-panel-tab-search .info-panel-tab-border,
.markdown-preview.markdown-preview .ty-show-search #info-panel-tab-search .info-panel-tab-border,
.markdown-preview.markdown-preview .active-tab-files #info-panel-tab-file .info-panel-tab-border,
.markdown-preview.markdown-preview .active-tab-outline #info-panel-tab-outline .info-panel-tab-border {
  border-radius: 2px;
  height: 3px;
}
.markdown-preview.markdown-preview #file-library-search-input {
  border-radius: 0 !important;
  top: 4px;
}
.markdown-preview.markdown-preview #close-outline-filter-btn {
  top: 8px;
  border: none !important;
  background: transparent !important;
}
.markdown-preview.markdown-preview .unibody-window .sidebar-content {
  top: 58px;
}
.markdown-preview.markdown-preview .native-window #file-library-search-input,
.markdown-preview.markdown-preview .unibody-window #file-library-search-input {
  border-radius: 5px !important;
  height: 28px;
}
.markdown-preview.markdown-preview .megamenu-menu-panel {
  scrollbar-width: none;
}
.markdown-preview.markdown-preview #theme-preview-grid {
  max-width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  justify-content: space-between;
  grid-gap: 32px;
}
@media (max-width: 1200px) {
  .markdown-preview.markdown-preview #theme-preview-grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
}
.markdown-preview.markdown-preview .theme-preview-div {
  border: 3px solid var(--border-color);
  padding: 4px;
  box-sizing: border-box;
  margin: 0;
  overflow: hidden;
  color: var(--color-1);
  border-radius: 8px;
}
.markdown-preview.markdown-preview .theme-preview-div:hover {
  border-color: var(--color-1);
}
.markdown-preview.markdown-preview .theme-preview-div.active {
  border-color: var(--color-2);
}
.markdown-preview.markdown-preview .theme-preview-div .fa {
  color: var(--color-2);
}
.markdown-preview.markdown-preview .theme-preview-content {
  width: 100%;
  height: 100%;
  border-radius: 5px;
}
.markdown-preview.markdown-preview #outline-btn:hover {
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview .context-menu {
  border-radius: 5px !important;
  padding: 5px !important;
  border: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview .context-menu li a {
  display: inline-block;
  width: 100%;
  border-radius: 5px !important;
}
.markdown-preview.markdown-preview .context-menu > .active a {
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview .tb43e-d6bd-dbe4y {
  background: var(--bg-color2);
}
.markdown-preview.markdown-preview #md-searchpanel input,
.markdown-preview.markdown-preview #md-replace-type-label,
.markdown-preview.markdown-preview #search-panel-replace-btn,
.markdown-preview.markdown-preview .modal-content,
.markdown-preview.markdown-preview .modal-dialog,
.markdown-preview.markdown-preview .modal-title,
.markdown-preview.markdown-preview .modal-content {
  border-radius: 5px;
}
.markdown-preview.markdown-preview .modal-content {
  padding: 6px;
}
.markdown-preview.markdown-preview #sidebar-files-menu li a {
  padding-top: 5px;
  padding-bottom: 5px;
}
.markdown-preview.markdown-preview #sidebar-files-menu .file-action-item {
  line-height: 30px;
}
.markdown-preview.markdown-preview #footer-word-count-info {
  border-radius: 5px;
  padding: 6px;
  border: 1px solid var(--border-color);
}
.markdown-preview.markdown-preview #footer-word-count-info table {
  border-radius: 0;
}
.markdown-preview.markdown-preview #footer-word-count-info table tr > td:first-of-type {
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
}
.markdown-preview.markdown-preview #footer-word-count-info table tr > td:last-of-type {
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}
.markdown-preview.markdown-preview #typora-quick-open {
  background-color: var(--bg-color);
  border: 1px solid var(--border-color);
  padding: 12px;
  border-radius: 8px;
}
.markdown-preview.markdown-preview .typora-quick-open-item,
.markdown-preview.markdown-preview .md-hover-tip .code-tooltip-content {
  border-radius: 5px;
  overflow-x: scroll;
}
.markdown-preview.markdown-preview .code-tooltip.md-hover-tip,
.markdown-preview.markdown-preview .md-hover-tip .code-tooltip-content,
.markdown-preview.markdown-preview .md-arrow::after {
  border-bottom-color: var(--color-1) !important;
  background: var(--color-1) !important;
  box-shadow: 0 1px 4px var(--color-1) !important;
}
.markdown-preview.markdown-preview .md-hover-tip .md-arrow::after {
  box-shadow: none !important;
}
.markdown-preview.markdown-preview .md-hover-tip .code-tooltip-content:hover a {
  color: var(--color-2) !important;
}
.markdown-preview.markdown-preview .sidebar {
  position: relative;
}
.markdown-preview.markdown-preview .sidebar:after {
  content: '';
  display: block;
  position: absolute;
  width: 1px;
  height: 100%;
  right: -24px;
  top: 0;
  background: var(--border-color);
}
.markdown-preview.markdown-preview .ty-preferences a {
  font-size: 12px !important;
  color: var(--color-1);
  text-decoration: none;
}
.markdown-preview.markdown-preview .ty-preferences a:hover {
  text-decoration: underline;
  color: var(--color-2);
}
.markdown-preview.markdown-preview .export-item.active,
.markdown-preview.markdown-preview .export-items-list-control {
  border-radius: 3px !important;
}
.markdown-preview.markdown-preview .nav-group-item {
  border-radius: 5px !important;
}
.markdown-preview.markdown-preview .input-group table,
.markdown-preview.markdown-preview .export-detail {
  border-radius: 5px !important;
}
.markdown-preview.markdown-preview .export-detail .file-input input {
  padding-left: 8px !important;
  height: 30px !important;
  border-color: var(--border-color) !important;
}
.markdown-preview.markdown-preview .search-input {
  padding: 6px 12px !important;
  border-color: var(--border-color) !important;
}
.markdown-preview.markdown-preview .label-input-group div {
  border: none !important;
  margin: 0 !important;
  padding: 0 !important;
}
.markdown-preview.markdown-preview .label-input-group div pre {
  border-radius: 5px !important;
  font-size: 14px !important;
}
.markdown-preview.markdown-preview .input-group-content {
  border-radius: 5px !important;
}
.markdown-preview.markdown-preview .input-group-content table {
  margin-top: 4px;
}
.markdown-preview.markdown-preview .label-hint svg {
  margin-right: 3px;
}
.markdown-preview.markdown-preview .export-detail .input-group-content > .row {
  margin-right: 16px;
}
.markdown-preview.markdown-preview .export-detail,
.markdown-preview.markdown-preview .export-item.active {
  color: var(--text-color) !important;
}
.markdown-preview.markdown-preview .export-item.active {
  color: var(--color-1) !important;
}
.markdown-preview.markdown-preview #md-notification {
  top: -6px;
}
.markdown-preview.markdown-preview .md-show-hint::after {
  background: var(--color-1-0-b) !important;
  border-radius: 3px;
  box-shadow: 0 0 5px var(--color-1) !important;
}
.markdown-preview.markdown-preview .md-diagram-panel {
  width: 100% !important;
  max-width: 100% !important;
  border-radius: 8px;
  margin-top: 20px;
  border-color: var(--border-color) !important;
  background: var(--bg-color2) !important;
}
.markdown-preview.markdown-preview .md-diagram {
  width: 100% !important;
  max-width: 100% !important;
  margin-left: 0 !important;
  margin-right: 0 !important;
}
.markdown-preview.markdown-preview .md-diagram-panel > svg {
  width: 100% !important;
  max-width: 100% !important;
}
@media print {
  .markdown-preview.markdown-preview table,
  .markdown-preview.markdown-preview pre {
    page-break-inside: avoid;
  }
  .markdown-preview.markdown-preview pre {
    word-wrap: break-word;
  }
}
@media screen and (max-width: 768px) {
  .markdown-preview.markdown-preview #write {
    padding: 0 32px 32px;
  }
  .markdown-preview.markdown-preview h2.md-focus:before,
  .markdown-preview.markdown-preview h3.md-focus:before,
  .markdown-preview.markdown-preview h4.md-focus:before,
  .markdown-preview.markdown-preview h5.md-focus:before,
  .markdown-preview.markdown-preview h6.md-focus:before,
  .markdown-preview.markdown-preview h2:before,
  .markdown-preview.markdown-preview h3:before,
  .markdown-preview.markdown-preview h4:before,
  .markdown-preview.markdown-preview h5:before,
  .markdown-preview.markdown-preview h6:before {
    left: -30px;
  }
}
@media screen and (min-width: 769px) and (max-width: 1280px) {
  .markdown-preview.markdown-preview #write {
    padding: 0 64px 32px;
  }
}
:root {
  --bg-color: #fafafa;
  --bg-color2: #ededef;
  --text-color: #000;
  --text-grey: #666;
  --text-code: #2f479f;
  --title-color: #070909;
  --border-color: #d2d2d2;
  --color-1: #3e69d7;
  --color-1-0-a: rgba(62, 105, 215, 0.15);
  --color-1-0-b: rgba(62, 105, 215, 0.06);
  --color-2: #f59102;
  --color-2-0-a: rgba(245, 145, 2, 0.15);
  --color-2-0-b: rgba(245, 145, 2, 0.06);
  --color-2-0-c: rgba(245, 145, 2, 0.3);
  --color-3: #03b736;
  --color-3-0-a: rgba(3, 183, 54, 0.15);
  --color-3-0-b: rgba(3, 183, 54, 0.06);
  --color-4: #8250df;
  --color-4-0-a: rgba(130, 80, 223, 0.15);
  --color-4-0-b: rgba(130, 80, 223, 0.06);
  --color-5: #e30f2e;
  --color-5-0-a: rgba(227, 15, 46, 0.15);
  --color-5-0-b: rgba(227, 15, 46, 0.06);
  --md-char-color: rgba(72, 93, 108, 0.75);
  --meta-content-color: var(--md-char-color);
  --primary-color: var(--color-1);
  --primary-btn-border-color: var(--bg-color2);
  --primary-btn-text-color: var(--text-color);
  --window-border: 1px solid var(--border-color);
  --active-file-bg-color: var(--bg-color2);
  --active-file-text-color: var(--color-1);
  --active-file-border-color: var(--color-1);
  --side-bar-bg-color: var(--bg-color);
  --item-hover-bg-color: var(--bg-color2);
  --item-hover-text-color: var(--color-1);
  --select-text-bg-color: var(--color-1-0-a);
  --monospace: monospace;
  /*--mermaid-theme: neutral;*/
  /*or base, dark, forest, neutral, night*/
  --mermaid-font-family: "trebuchet ms", verdana, arial, sans-serif;
  --mermaid-sequence-numbers: on;
  --mermaid-flowchart-curve: linear;
  --mermaid--gantt-left-padding: 75;
}

      </style>
      <!-- The content below will be included at the end of the <head> element. --><script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function () {
    // your code here
  });
</script></head><body for="html-export">
    
    
      <div class="crossnote markdown-preview  ">
      
<h1 id="并发编程">并发编程 </h1>
<h2 id="索引">索引 </h2>
<ol>
<li>
<p><a href="#%E4%BF%A1%E5%8F%B7">信号</a><br>
<a href="#%E4%BF%A1%E5%8F%B7%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81">代码示例</a></p>
<ul>
<li>kill<br>
向指定的进程发送信号</li>
<li>pause<br>
用于使调用进程阻塞，直到接收到一个信号。</li>
<li>signal<br>
设置信号处理程序</li>
<li>sigemptyset<br>
初始化一个信号集并将其清空</li>
<li>sigaddset<br>
将指定的信号添加到信号集中</li>
<li>sigprocmask<br>
更改进程的信号屏蔽字</li>
<li>sigfillset<br>
将信号集中的所有信号设置为有效</li>
<li>struct sigaction<br>
用于定义信号处理程序的行为</li>
<li>sigaction<br>
更改信号处理程序的行为</li>
<li>union sigval<br>
用于在信号处理程序和信号发送函数之间传递数据</li>
<li>sigqueue<br>
向指定的进程发送信号，并附带一个 union sigval 类型的数据</li>
</ul>
</li>
<li>
<p><a href="#%E7%AE%A1%E9%81%93">管道</a><br>
<a href="#%E7%AE%A1%E9%81%93%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81">相关代码</a></p>
<ul>
<li>pipe<br>
创建一个管道</li>
<li>mkfifo<br>
创建一个命名管道（FIFO）</li>
</ul>
</li>
<li>
<p><a href="#%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97">消息队列</a><br>
<a href="#%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81">代码示例</a><br>
消息队列是一种用于进程间通信的机制，它允许一个进程向另一个进程发送消息。</p>
<ul>
<li>key_t<br>
用于表示 IPC 键</li>
<li>ftok<br>
生成一个 IPC 键</li>
<li>msgget<br>
获取一个消息队列的标识符</li>
<li>msgsnd<br>
向消息队列发送消息</li>
<li>msgrcv<br>
从消息队列接收消息</li>
<li>msgctl<br>
控制消息队列</li>
</ul>
</li>
<li>
<p><a href="#%E5%85%B1%E4%BA%AB%E5%86%85%E5%AD%98">共享内存</a><br>
<a href="#%E5%85%B1%E4%BA%AB%E5%86%85%E5%AD%98%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81">代码示例</a><br>
共享内存是一种用于进程间通信的机制，它允许一个进程向另一个进程共享数据。</p>
<ul>
<li>shmget<br>
获取一个共享内存段的标识符</li>
<li>shmat<br>
将共享内存段附加到调用进程的地址空间</li>
<li>shmdt<br>
将共享内存段从调用进程的地址空间分离</li>
<li>struct shmid_ds<br>
用于描述共享内存段状态的结构</li>
<li>shmctl<br>
控制共享内存段</li>
</ul>
</li>
<li>
<p><a href="#%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%BB%84">信号量组</a><br>
<a href="#%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%BB%84%E7%9B%B8%E5%85%B3%E4%BB%A3%E7%A0%81">代码示例</a></p>
<ul>
<li>semget<br>
获取一个信号量的标识符</li>
<li>semctl<br>
控制信号量</li>
<li>struct sembuf<br>
用于表示信号量操作</li>
<li>semop<br>
执行信号量操作</li>
</ul>
</li>
<li>
<p>进程</p>
<ul>
<li>pid_t<br>
表示进程 ID</li>
<li>fork<br>
创建一个子进程</li>
<li>waitpid<br>
等待一个或多个子进程终止</li>
</ul>
</li>
<li>
<p>线程</p>
<ul>
<li>pthread_t<br>
用于表示 POSIX 线程（也称为 Pthreads）的标识符</li>
<li>pthread_create<br>
创建一个新的线程</li>
<li>pthread_exit<br>
终止当前线程</li>
<li>pthread_join<br>
等待一个线程终止</li>
<li>pthread_cleanup_push<br>
用于注册一个清理函数</li>
<li>pthread_cleanup_pop<br>
用于取消一个清理函数的注册</li>
<li>pthread_cancel<br>
用于取消一个线程</li>
<li>pthread_setcancelstate<br>
用于设置线程的取消状态</li>
</ul>
</li>
<li>
<p>其他</p>
<ul>
<li>access</li>
<li>chmod</li>
<li>umask<br>
设置文件模式创建屏蔽字为mask，并返回之前的文件模式创建屏蔽字。</li>
</ul>
</li>
</ol>
<h2 id="信号">信号 </h2>
<h3 id="信号相关函数">信号相关函数 </h3>
<ul>
<li>
<p>signal<br>
用于设置一个信号的处理函数</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-typedef">typedef</span> <span class="token keyword keyword-void">void</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token class-name">sighandler_t</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">sighandler_t</span> <span class="token function">signal</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> signum<span class="token punctuation">,</span> <span class="token class-name">sighandler_t</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 signum 是要处理的信号的编号，</span>
<span class="token comment">// handler 是信号处理函数的指针。</span>
<span class="token comment">// signal 函数返回值为信号处理函数的指针，表示旧的信号处理函数。</span>
<span class="token comment">// 如果 signal 函数调用失败，则返回 SIG_ERR。</span>
</code></pre></li>
<li>
<p>kill<br>
向指定的进程发送信号</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">kill</span><span class="token punctuation">(</span><span class="token class-name">pid_t</span> pid<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> sig<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 pid 是要发送信号的进程的进程 ID，sig 是要发送的信号的编号。</span>
<span class="token comment">// kill 函数返回值为 0 表示成功，</span>
<span class="token comment">// 返回值为 -1 表示失败，并设置 errno 以指示错误原因。</span>
</code></pre></li>
<li>
<p>sigemptyset<br>
用于初始化一个信号集并将其清空。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">sigemptyset</span><span class="token punctuation">(</span><span class="token class-name">sigset_t</span><span class="token operator">*</span> set<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 set 是指向要清空的信号集的指针。</span>
<span class="token comment">// sigemptyset函数返回值为 0 表示成功，返回值为</span>
<span class="token comment">// -1 表示失败，并设置 errno 以指示错误原因。</span>
</code></pre></li>
<li>
<p>sigaddset<br>
用于将指定的信号添加到信号集中</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token class-name">sigset_t</span><span class="token operator">*</span> set<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> signum<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 set 是指向要添加信号的信号集的指针，signum 是要添加的信号的编号。</span>
<span class="token comment">// sigaddset 函数返回值为 0 表示成功，</span>
<span class="token comment">// 返回值为 -1 表示失败，并设置 errno 以指示错误原因。</span>
</code></pre></li>
<li>
<p>sigprocmask<br>
用于更改进程的信号屏蔽字</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">sigprocmask</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> how<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token class-name">sigset_t</span><span class="token operator">*</span> set<span class="token punctuation">,</span> <span class="token class-name">sigset_t</span><span class="token operator">*</span> oldset<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 参数 how 指定如何更改信号屏蔽字，set 是指向要设置的信号集的指针，</span>
<span class="token comment">// oldset 是指向保存当前信号屏蔽字的指针。</span>
<span class="token comment">// sigprocmask 函数返回值为 0 表示成功，</span>
<span class="token comment">// 返回值为 - 1 表示失败，并设置 errno 以指示错误原因。</span>
</code></pre></li>
<li>
<p>sigfillset<br>
将信号集中的所有信号设置为有效</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">sigfillset</span><span class="token punctuation">(</span><span class="token class-name">sigset_t</span><span class="token operator">*</span> set<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 set 是指向要填充的信号集的指针。</span>
<span class="token comment">// sigfillset 函数返回值为 0 表示成功，</span>
<span class="token comment">// 返回值为 -1 表示失败，并设置 errno 以指示错误原因。</span>
</code></pre></li>
<li>
<p>sigaction</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">sigaction</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> signum<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-struct">struct</span> <span class="token class-name">sigaction</span><span class="token operator">*</span> act<span class="token punctuation">,</span> <span class="token keyword keyword-struct">struct</span> <span class="token class-name">sigaction</span><span class="token operator">*</span> oldact<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 signum 是要更改的信号的编号，</span>
<span class="token comment">// act 是指向 struct sigaction 结构体的指针，用于设置信号处理程序的行为，</span>
<span class="token comment">// oldact 是指向 struct sigaction 结构体的指针，用于保存旧的信号处理程序的行为。</span>
<span class="token comment">// sigaction 函数返回值为 0 表示成功，</span>
<span class="token comment">// 返回值为 -1 表示失败，并设置 errno 以指示错误原因</span>

<span class="token keyword keyword-struct">struct</span> <span class="token class-name">sigaction</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-void">void</span> <span class="token punctuation">(</span><span class="token operator">*</span>sa_handler<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 指向信号处理程序的指针。如果设置为 SIG_IGN，则忽略信号；</span>
                               <span class="token comment">// 如果设置为 SIG_DFL，则使用默认处理程序。</span>
    <span class="token keyword keyword-void">void</span> <span class="token punctuation">(</span><span class="token operator">*</span>sa_sigaction<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span><span class="token punctuation">,</span> <span class="token class-name">siginfo_t</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token keyword keyword-void">void</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 指向扩展信号处理程序的指针。</span>
    <span class="token class-name">sigset_t</span> sa_mask<span class="token punctuation">;</span>   <span class="token comment">// 信号屏蔽字，用于指定在信号处理程序执行期间要屏蔽的信号。</span>
    <span class="token keyword keyword-int">int</span>  sa_flags<span class="token punctuation">;</span>               <span class="token comment">// 标志，用于控制信号处理程序的行为</span>
    <span class="token keyword keyword-void">void</span> <span class="token punctuation">(</span><span class="token operator">*</span>sa_restorer<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword keyword-void">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 已弃用，不应使用。</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

</code></pre></li>
<li>
<p>sigqueue<br>
用于向指定的进程发送信号</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">sigqueue</span><span class="token punctuation">(</span><span class="token class-name">pid_t</span> pid<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> sig<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-union">union</span> sigval value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 参数 pid 是要发送信号的进程的进程 ID，sig 是要发送的信号的编号，</span>
<span class="token comment">// value 是一个 union sigval 类型的数据。</span>
<span class="token comment">// sigqueue 函数返回值为 0 表示成功，</span>
<span class="token comment">// 返回值为 -1 表示失败，并设置 errno 以指示错误原因。</span>

<span class="token keyword keyword-union">union</span> sigval
<span class="token punctuation">{</span>
    <span class="token keyword keyword-int">int</span>   sival_int<span class="token punctuation">;</span>
    <span class="token keyword keyword-void">void</span><span class="token operator">*</span> sival_ptr<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></li>
</ul>
<h3 id="信号相关代码">信号相关代码 </h3>
<p>忽略信号</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token comment">// 忽略信号</span>
<span class="token function">signal</span><span class="token punctuation">(</span>SIGINT<span class="token punctuation">,</span> SIG_IGN<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">signal</span><span class="token punctuation">(</span>SIGKILL<span class="token punctuation">,</span> SIG_IGN<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><hr>
<p>设置响应函数</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>


<span class="token keyword keyword-void">void</span> <span class="token function">handler</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> signum<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 设置信号的响应函数</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGINT<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGQUIT<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGHUP<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGKILL<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 尝试捕获9号信号，并设置了响应函数，但是没有效果</span>

    <span class="token function">signal</span><span class="token punctuation">(</span><span class="token number">34</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span><span class="token number">35</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span><span class="token number">36</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>设置阻塞函数</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>


<span class="token keyword keyword-void">void</span> <span class="token function">handler</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> signum<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d[%d]\n"</span><span class="token punctuation">,</span> <span class="token constant">__LINE__</span><span class="token punctuation">,</span> <span class="token function">getpid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 设置信号的响应函数</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGINT<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGQUIT<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGHUP<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span>SIGKILL<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 尝试捕获9号信号，并设置了响应函数，但是没有效果</span>

    <span class="token function">signal</span><span class="token punctuation">(</span><span class="token number">34</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span><span class="token number">35</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">signal</span><span class="token punctuation">(</span><span class="token number">36</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 定义一个信号集，用于存储需要阻塞的信号</span>
    <span class="token class-name">sigset_t</span> set<span class="token punctuation">;</span>

    <span class="token comment">// 清空信号集</span>
    <span class="token function">sigemptyset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 添加需要阻塞的信号</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> SIGINT<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> SIGQUIT<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> SIGHUP<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> SIGKILL<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 尝试添加9号信号，但是没有效果</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> <span class="token number">34</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> <span class="token number">35</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sigaddset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>set<span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 定义一个信号集，用于存储原有的信号集</span>
    <span class="token class-name">sigset_t</span> oldset<span class="token punctuation">;</span>
    <span class="token function">sigemptyset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>oldset<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 设置需要阻塞的信号并把信号进行阻塞挂起</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">sigprocmask</span><span class="token punctuation">(</span>SIG_SETMASK<span class="token punctuation">,</span>   <span class="token comment">// 替换现在正阻塞的信号集合</span>
                    <span class="token operator">&amp;</span>set<span class="token punctuation">,</span>   <span class="token comment">// 新的信号集合， 配合 SIG_SETMASK 选项表示我需要设置阻塞的信号集合</span>
                    <span class="token operator">&amp;</span>oldset<span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">// 旧的信号集合，用于保存设置之前的阻塞信号集合，方便后期进行恢复</span>
    <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"sigprocmask"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword keyword-return">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"开始阻塞信号\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 假设在处理一些系统资源需要避免被信号打断</span>

    <span class="token comment">// 恢复原有的信号集</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">sigprocmask</span><span class="token punctuation">(</span>SIG_SETMASK<span class="token punctuation">,</span> <span class="token operator">&amp;</span>oldset<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"sigprocmask"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword keyword-return">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"恢复原有的信号集\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>发送所有信号</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>


<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>argc <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">" 参数异常 \n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 循环发送所有信号到指定进程中</span>
    <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">64</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 跳过所有不能捕获的信号</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> <span class="token number">9</span> <span class="token operator">||</span> i <span class="token operator">==</span> <span class="token number">19</span> <span class="token operator">||</span> i <span class="token operator">==</span> <span class="token number">33</span> <span class="token operator">||</span> i <span class="token operator">==</span> <span class="token number">32</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword keyword-continue">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token function">kill</span><span class="token punctuation">(</span><span class="token function">atoi</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>接收者信号响应函数</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>


<span class="token keyword keyword-void">void</span> <span class="token function">func_info</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> sigNum<span class="token punctuation">,</span> <span class="token class-name">siginfo_t</span><span class="token operator">*</span> Info<span class="token punctuation">,</span> <span class="token keyword keyword-void">void</span><span class="token operator">*</span> ptr<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"收到信号%d\n"</span><span class="token punctuation">,</span> sigNum<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"发送者ID:%d\n"</span><span class="token punctuation">,</span> Info<span class="token operator">-&gt;</span>si_pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"发送者UID:%d\n"</span><span class="token punctuation">,</span> Info<span class="token operator">-&gt;</span>si_uid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"发送者信号:%d\n"</span><span class="token punctuation">,</span> Info<span class="token operator">-&gt;</span>si_signo<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"发送者信息:%d\n"</span><span class="token punctuation">,</span> Info<span class="token operator">-&gt;</span>si_int<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 定义一个信号响应的结构体用于设置特殊响应函数</span>
    <span class="token keyword keyword-struct">struct</span> <span class="token class-name">sigaction</span> act <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token punctuation">.</span>sa_sigaction <span class="token operator">=</span> func_info<span class="token punctuation">,</span>    <span class="token comment">// 设置结构体中拓展响应函数的入口地址</span>
        <span class="token punctuation">.</span>sa_flags     <span class="token operator">=</span> SA_SIGINFO<span class="token punctuation">,</span>   <span class="token comment">// 设置标志位 , 表示使用拓展响应函数</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token comment">// 设置信号2的响应函数</span>
    <span class="token function">sigaction</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>act<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"[%d]\n"</span><span class="token punctuation">,</span> <span class="token function">getpid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">pause</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>发送者 发送信号并携带数据</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;signal.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>


<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-union">union</span> sigval value <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">.</span>sival_int <span class="token operator">=</span> <span class="token function">atoi</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token function">sigqueue</span><span class="token punctuation">(</span><span class="token function">atoi</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"[%d][%d]\n"</span><span class="token punctuation">,</span> <span class="token function">getpid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">getuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="管道">管道 </h2>
<h3 id="管道相关函数">管道相关函数 </h3>
<ul>
<li>
<p>pipe<br>
用于创建一个管道，该管道可用于进程间通信。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> pipefd<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// pipefd[0] 用于读取数据，pipefd[1] 用于写入数据。</span>
<span class="token comment">// 如果pipe函数调用成功，它将返回0；如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
<span class="token comment">// 需要注意的是，管道是单向的，即数据只能从一个进程流向另一个进程。</span>
<span class="token comment">// 如果需要双向通信，需要创建两个管道。</span>
</code></pre></li>
<li>
<p>mkfifo<br>
用于创建一个命名管道（FIFO）。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">mkfifo</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-char">char</span><span class="token operator">*</span> pathname<span class="token punctuation">,</span> <span class="token class-name">mode_t</span> mode<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建一个命名管道，其路径名由pathname参数指定。</span>
<span class="token comment">// mode参数指定了命名管道的权限，可以使用umask函数来修改。</span>
<span class="token comment">// 如果mkfifo函数调用成功，它将返回0；</span>
<span class="token comment">// 如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
</code></pre></li>
</ul>
<h3 id="管道相关代码">管道相关代码 </h3>
<p>匿名管道</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>

<span class="token comment">// 子进程</span>
<span class="token keyword keyword-void">void</span> <span class="token function">ChildProcess</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> writefd<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-char">char</span> SendMsg<span class="token punctuation">[</span><span class="token number">128</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">fgets</span><span class="token punctuation">(</span>SendMsg<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>SendMsg<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 发送消息给父进程</span>
        <span class="token function">write</span><span class="token punctuation">(</span>writefd<span class="token punctuation">,</span> SendMsg<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>SendMsg<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 父进程</span>
<span class="token keyword keyword-void">void</span> <span class="token function">ParentProcess</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> readfd<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 读取子进程发来的消息</span>
        <span class="token keyword keyword-char">char</span> buf<span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"__%d__\n"</span><span class="token punctuation">,</span> <span class="token constant">__LINE__</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">read</span><span class="token punctuation">(</span>readfd<span class="token punctuation">,</span> buf<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"buf = %s\n"</span><span class="token punctuation">,</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-int">int</span> pipefd<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>   <span class="token comment">// 定义好一个数组用于存储两个文件描述符符（匿名管道的输入和输出端）</span>

    <span class="token comment">// 创建匿名管道</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">pipe</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"pipe error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    <span class="token comment">// 产生子进程</span>
    <span class="token keyword keyword-int">int</span> pid <span class="token operator">=</span> <span class="token function">fork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>pid <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"fork error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword keyword-else">else</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 子进程 传入写端描述符</span>
        <span class="token function">ChildProcess</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 父进程  传入读端描述符</span>
        <span class="token function">ParentProcess</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>有名管道<br>
写端</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">FIFO_PATH</span> <span class="token string">"/tmp/fifo2"</span></span>


<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 创建管道</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">access</span><span class="token punctuation">(</span>FIFO_PATH<span class="token punctuation">,</span> W_OK <span class="token operator">|</span> F_OK<span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">// 判断文件是否已经存在 以及是否拥有可写权限</span>
    <span class="token punctuation">{</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">access</span><span class="token punctuation">(</span>FIFO_PATH<span class="token punctuation">,</span> F_OK<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">mkfifo</span><span class="token punctuation">(</span>FIFO_PATH<span class="token punctuation">,</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span>
            <span class="token function">chmod</span><span class="token punctuation">(</span>FIFO_PATH<span class="token punctuation">,</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 打开管道</span>
    <span class="token keyword keyword-int">int</span> fd_write <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span>FIFO_PATH<span class="token punctuation">,</span> O_RDWR<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>fd_write <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"open fifo error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 写入数据</span>
    <span class="token keyword keyword-char">char</span> buf<span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"请输入数据：\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">fgets</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">write</span><span class="token punctuation">(</span>fd_write<span class="token punctuation">,</span> buf<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>读端</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">FIFO_RECV</span> <span class="token string">"/tmp/fifo2"</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">initFifo</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-char">char</span><span class="token operator">*</span> fifoPath<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 判断是否存在</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">access</span><span class="token punctuation">(</span>fifoPath<span class="token punctuation">,</span> F_OK<span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">// 判断发送管道是否存在</span>
    <span class="token punctuation">{</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">mkfifo</span><span class="token punctuation">(</span>fifoPath<span class="token punctuation">,</span> <span class="token number">0666</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>   <span class="token comment">// 如果不存在则创建</span>
        <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"mkfifo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword keyword-return">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"管道已经创建或已经存在.. \n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 打开管道文件</span>
    <span class="token keyword keyword-int">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span>fifoPath<span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 只读打开</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>fd <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"open fifo error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword keyword-return">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"管道文件已经打开.. \n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> fd<span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>

    <span class="token comment">// 初始化管道文件</span>
    <span class="token keyword keyword-int">int</span> fd <span class="token operator">=</span> <span class="token function">initFifo</span><span class="token punctuation">(</span>FIFO_RECV<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword keyword-char">char</span> buf<span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword keyword-int">int</span>  ret       <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> buf<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>ret <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"read"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword keyword-return">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword keyword-else">else</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"写端已经关闭..\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword keyword-break">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"读取到数据: %s\n"</span><span class="token punctuation">,</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 关闭管道文件</span>
    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="消息队列">消息队列 </h2>
<h3 id="消息队列相关函数">消息队列相关函数 </h3>
<ul>
<li>
<p>ftok<br>
用于将一个路径名和一个项目ID转换为一个System V IPC键。这个键可以用于创建消息队列、信号量或共享内存。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token class-name">key_t</span> <span class="token function">ftok</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-char">char</span><span class="token operator">*</span> pathname<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> proj_id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// pathname参数是一个路径名，proj_id参数是一个项目ID。</span>
<span class="token comment">// ftok函数使用pathname和proj_id计算一个键，这个键可以用于创建消息队列、信号量或共享内存。</span>
<span class="token comment">// 如果ftok函数调用成功，它将返回一个键；</span>
<span class="token comment">// 如果调用失败，它将返回(key_t)-1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>msgget<br>
用于获取一个消息队列的标识符。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">msgget</span><span class="token punctuation">(</span><span class="token class-name">key_t</span> key<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> msgflg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// key参数是一个键，msgflg参数是一些标志。</span>
<span class="token comment">// 如果msgget函数调用成功，它将返回一个消息队列的标识符；</span>
<span class="token comment">// 如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>msgsnd<br>
用于向消息队列发送消息。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">msgsnd</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> msqid<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-void">void</span><span class="token operator">*</span> msgp<span class="token punctuation">,</span> <span class="token class-name">size_t</span> msgsz<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> msgflg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// msqid参数是一个消息队列的标识符，msgp参数是一个指向消息的指针，</span>
<span class="token comment">// msgsz参数是消息的大小，msgflg参数是一些标志。</span>
<span class="token comment">// 如果msgsnd函数调用成功，它将返回0；如果调用失败，它将返回 - 1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>msgrcv<br>
用于从消息队列中接收消息。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token class-name">ssize_t</span> <span class="token function">msgrcv</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> msqid<span class="token punctuation">,</span> <span class="token keyword keyword-void">void</span><span class="token operator">*</span> msgp<span class="token punctuation">,</span> <span class="token class-name">size_t</span> msgsz<span class="token punctuation">,</span> <span class="token keyword keyword-long">long</span> <span class="token keyword keyword-int">int</span> msgtyp<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> msgflg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// msqid参数是一个消息队列的标识符，msgp参数是一个指向消息的指针，</span>
<span class="token comment">// msgsz参数是消息的大小，msgtyp参数是消息的类型，msgflg参数是一些标志</span>
<span class="token comment">// 如果msgrcv函数调用成功，它将返回接收到的消息的大小；</span>
<span class="token comment">// 如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>msgctl<br>
用于控制消息队列。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">msgctl</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> msqid<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> cmd<span class="token punctuation">,</span> <span class="token keyword keyword-struct">struct</span> <span class="token class-name">msqid_ds</span><span class="token operator">*</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// msqid参数是一个消息队列的标识符，</span>
<span class="token comment">// cmd参数是一个命令，buf参数是一个指向msqid_ds结构的指针。</span>

<span class="token comment">// cmd参数可以是以下值之一：</span>
<span class="token comment">// IPC_STAT：获取消息队列的状态信息，并将信息存储在buf指向的结构中。</span>
<span class="token comment">// IPC_SET：设置消息队列的状态信息，使用buf指向的结构中的信息。</span>
<span class="token comment">// IPC_RMID：删除消息队列。</span>

<span class="token comment">// 如果msgctl函数调用成功，它将返回0；</span>
<span class="token comment">// 如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
</code></pre></li>
</ul>
<h3 id="消息队列相关代码">消息队列相关代码 </h3>
<p>消息队列发送</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/ipc.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/msg.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">PATHNAME</span> <span class="token string">"/home/china"</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">PROJ_ID</span>  <span class="token expression"><span class="token number">101</span></span></span>

<span class="token keyword keyword-typedef">typedef</span> <span class="token keyword keyword-struct">struct</span> <span class="token class-name">msgbuf</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-long">long</span> mtype<span class="token punctuation">;</span>       <span class="token comment">/* message type, must be &gt; 0 */</span>
    <span class="token keyword keyword-char">char</span> mtext<span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">/* message data */</span>
<span class="token punctuation">}</span> MyType<span class="token punctuation">;</span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 通过ftok 获得 通信对象的key</span>
    <span class="token class-name">key_t</span> key <span class="token operator">=</span> <span class="token function">ftok</span><span class="token punctuation">(</span>PATHNAME<span class="token punctuation">,</span> PROJ_ID<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"key = %x\n"</span><span class="token punctuation">,</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 通过key 获得通信对象</span>
    <span class="token keyword keyword-int">int</span> msgid <span class="token operator">=</span> <span class="token function">msgget</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> IPC_CREAT <span class="token operator">|</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"msgid = %d\n"</span><span class="token punctuation">,</span> msgid<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> i <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 发送消息</span>
        MyType msg <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">.</span>mtype <span class="token operator">=</span> i<span class="token punctuation">}</span><span class="token punctuation">;</span>   <span class="token comment">// 设置发送的消息类型为1;</span>

        <span class="token function">snprintf</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span>mtext<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span>mtext<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"[%d]类型消息"</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 发送消息时消息的尺寸应该是正文的尺寸，去除标识的大小</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">msgsnd</span><span class="token punctuation">(</span>msgid<span class="token punctuation">,</span> <span class="token operator">&amp;</span>msg<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span>mtext<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"msgsnd"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword keyword-return">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>消息队列接收</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;bits/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/ipc.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">__USE_GNU</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/msg.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">PATHNAME</span> <span class="token string">"/home/china"</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">PROJ_ID</span>  <span class="token expression"><span class="token number">101</span></span></span>

<span class="token keyword keyword-typedef">typedef</span> <span class="token keyword keyword-struct">struct</span>
<span class="token punctuation">{</span>
    <span class="token keyword keyword-long">long</span> mtype<span class="token punctuation">;</span>       <span class="token comment">/* message type, must be &gt; 0 */</span>
    <span class="token keyword keyword-char">char</span> mtext<span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">/* message data */</span>
<span class="token punctuation">}</span> MyType<span class="token punctuation">;</span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 通过ftok 获得 通信对象的key</span>
    <span class="token class-name">key_t</span> key <span class="token operator">=</span> <span class="token function">ftok</span><span class="token punctuation">(</span>PATHNAME<span class="token punctuation">,</span> PROJ_ID<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"key = %x\n"</span><span class="token punctuation">,</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 通过key 获得通信对象</span>
    <span class="token keyword keyword-int">int</span> msgid <span class="token operator">=</span> <span class="token function">msgget</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> IPC_CREAT <span class="token operator">|</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"msgid = %d\n"</span><span class="token punctuation">,</span> msgid<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 接收消息</span>
    MyType msg <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>   <span class="token comment">// 设置消息发送的正文内容</span>


    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">msgrcv</span><span class="token punctuation">(</span>msgid<span class="token punctuation">,</span> <span class="token operator">&amp;</span>msg<span class="token punctuation">,</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> MSG_EXCEPT<span class="token punctuation">)</span> <span class="token operator">&gt;</span>
        <span class="token number">0</span><span class="token punctuation">)</span>   <span class="token comment">// MSG_NOERROR 无视风险  IPC_NOWAIT  不阻塞</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"收到消息：%s\n"</span><span class="token punctuation">,</span> msg<span class="token punctuation">.</span>mtext<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"msgrcv error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">msgctl</span><span class="token punctuation">(</span>msgid<span class="token punctuation">,</span> IPC_RMID<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="共享内存">共享内存 </h2>
<h3 id="共享内存相关函数">共享内存相关函数 </h3>
<ul>
<li>
<p>shmget<br>
用于获取一个共享内存段的标识符。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">shmget</span><span class="token punctuation">(</span><span class="token class-name">key_t</span> key<span class="token punctuation">,</span> <span class="token class-name">size_t</span> size<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> shmflg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// key参数是一个键，size参数是共享内存段的大小，shmflg参数是一些标志。</span>
<span class="token comment">// 如果shmget函数调用成功，它将返回一个共享内存段的标识符；</span>
<span class="token comment">// 如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>shmat<br>
用于将共享内存段附加到调用进程的地址空间。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-void">void</span><span class="token operator">*</span> <span class="token function">shmat</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> shmid<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-void">void</span><span class="token operator">*</span> shmaddr<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> shmflg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// shmid参数是一个共享内存段的标识符，</span>
<span class="token comment">// shmaddr参数是一个建议的附加地址，shmflg参数是一些标志。</span>
<span class="token comment">// 如果shmat函数调用成功，它将返回附加的共享内存段的地址；</span>
<span class="token comment">// 如果调用失败，它将返回(void*)-1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>shmdt<br>
用于将共享内存段从调用进程的地址空间分离。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">shmdt</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-void">void</span><span class="token operator">*</span> shmaddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// shmaddr参数是一个共享内存段的地址。</span>
<span class="token comment">// 如果shmdt函数调用成功，它将返回0；</span>
<span class="token comment">// 如果调用失败，它将返回 -1，并设置errno以指示错误原因。</span>
</code></pre></li>
<li>
<p>shmctl<br>
用于控制共享内存段。</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token keyword keyword-int">int</span> <span class="token function">shmctl</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> shmid<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> cmd<span class="token punctuation">,</span> <span class="token keyword keyword-struct">struct</span> <span class="token class-name">shmid_ds</span><span class="token operator">*</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// shmid参数是一个共享内存段的标识符，cmd参数是一个命令，buf参数是一个指向shmid_ds结构的指针。</span>

<span class="token comment">// cmd参数可以是以下值之一：</span>
<span class="token comment">// IPC_STAT：获取共享内存段的状态信息，并将信息存储在buf指向的结构中。</span>
<span class="token comment">// IPC_SET：设置共享内存段的状态信息，使用buf指向的结构中的信息。</span>
<span class="token comment">// IPC_RMID：删除共享内存段。</span>
</code></pre></li>
</ul>
<h3 id="共享内存相关代码">共享内存相关代码 </h3>
<p>共享内存发送</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/ipc.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/shm.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 获得key</span>
    <span class="token class-name">key_t</span> shmKey <span class="token operator">=</span> <span class="token function">ftok</span><span class="token punctuation">(</span><span class="token string">"./"</span><span class="token punctuation">,</span> <span class="token number">0x01</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>shmKey <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"ftok error "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"获得ipc对象的key :%x \n"</span><span class="token punctuation">,</span> shmKey<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 获得id</span>
    <span class="token keyword keyword-int">int</span> shmID <span class="token operator">=</span> <span class="token function">shmget</span><span class="token punctuation">(</span>shmKey<span class="token punctuation">,</span> <span class="token number">4096</span><span class="token punctuation">,</span> IPC_CREAT <span class="token operator">|</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>shmID <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"shm get error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"获得SHM的ID :%x \n"</span><span class="token punctuation">,</span> shmID<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 映射到本进程</span>
    <span class="token keyword keyword-char">char</span><span class="token operator">*</span> map <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword keyword-char">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">shmat</span><span class="token punctuation">(</span>shmID<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span><span class="token punctuation">)</span>map <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"shmat error "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"成功映射到本进程:%p \n"</span><span class="token punctuation">,</span> map<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 为所欲为</span>
    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">strlen</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"请输入需要发送的内容：\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">fgets</span><span class="token punctuation">(</span>map<span class="token punctuation">,</span> <span class="token number">4096</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    <span class="token comment">// 解除映射</span>
    <span class="token function">shmdt</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 删除通信对象</span>
    <span class="token function">shmctl</span><span class="token punctuation">(</span>shmID<span class="token punctuation">,</span> IPC_RMID<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><hr>
<p>接收</p>
<pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/ipc.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/shm.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>

<span class="token keyword keyword-int">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> argc<span class="token punctuation">,</span> <span class="token keyword keyword-char">char</span> <span class="token keyword keyword-const">const</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 获得key</span>
    <span class="token class-name">key_t</span> shmKey <span class="token operator">=</span> <span class="token function">ftok</span><span class="token punctuation">(</span><span class="token string">"./"</span><span class="token punctuation">,</span> <span class="token number">0x01</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>shmKey <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"ftok error "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"获得ipc对象的key :%x \n"</span><span class="token punctuation">,</span> shmKey<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 获得id</span>
    <span class="token keyword keyword-int">int</span> shmID <span class="token operator">=</span> <span class="token function">shmget</span><span class="token punctuation">(</span>shmKey<span class="token punctuation">,</span> <span class="token number">4096</span><span class="token punctuation">,</span> IPC_CREAT <span class="token operator">|</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>shmID <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"shm get error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"获得SHM的ID :%x \n"</span><span class="token punctuation">,</span> shmID<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 映射到本进程</span>
    <span class="token keyword keyword-char">char</span><span class="token operator">*</span> map <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword keyword-char">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">shmat</span><span class="token punctuation">(</span>shmID<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword keyword-int">int</span><span class="token punctuation">)</span>map <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"shmat error "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"成功映射到本进程:%p \n"</span><span class="token punctuation">,</span> map<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token comment">// 为所欲为</span>
    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token function">strlen</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"收到的内容：%s\n"</span><span class="token punctuation">,</span> map<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">memset</span><span class="token punctuation">(</span>map<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4096</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 清空共享内存</span>
        <span class="token punctuation">}</span>
        <span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    <span class="token comment">// 解除映射</span>
    <span class="token function">shmdt</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 删除通信对象</span>
    <span class="token function">shmctl</span><span class="token punctuation">(</span>shmID<span class="token punctuation">,</span> IPC_RMID<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-return">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="信号量组">信号量组 </h2>
<h3 id="信号量组相关函数">信号量组相关函数 </h3>
<h3 id="信号量组相关代码">信号量组相关代码 </h3>

      </div>
      <div class="md-sidebar-toc">
<div class="md-toc">
<details style="padding:0;;padding-left:0px;" open="">
        <summary class="md-toc-link-wrapper">
          <a href="#并发编程" class="md-toc-link"><p>并发编程</p>
</a>
          </summary>
        <div>
          <div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#索引" class="md-toc-link">
            <p>索引</p>

          </a></div><details style="padding:0;;padding-left:24px;" open="">
        <summary class="md-toc-link-wrapper">
          <a href="#信号" class="md-toc-link"><p>信号</p>
</a>
          </summary>
        <div>
          <div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#信号相关函数" class="md-toc-link">
            <p>信号相关函数</p>

          </a></div><div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#信号相关代码" class="md-toc-link">
            <p>信号相关代码</p>

          </a></div>
        </div>
      </details>
    <details style="padding:0;;padding-left:24px;" open="">
        <summary class="md-toc-link-wrapper">
          <a href="#管道" class="md-toc-link"><p>管道</p>
</a>
          </summary>
        <div>
          <div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#管道相关函数" class="md-toc-link">
            <p>管道相关函数</p>

          </a></div><div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#管道相关代码" class="md-toc-link">
            <p>管道相关代码</p>

          </a></div>
        </div>
      </details>
    <details style="padding:0;;padding-left:24px;" open="">
        <summary class="md-toc-link-wrapper">
          <a href="#消息队列" class="md-toc-link"><p>消息队列</p>
</a>
          </summary>
        <div>
          <div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#消息队列相关函数" class="md-toc-link">
            <p>消息队列相关函数</p>

          </a></div><div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#消息队列相关代码" class="md-toc-link">
            <p>消息队列相关代码</p>

          </a></div>
        </div>
      </details>
    <details style="padding:0;;padding-left:24px;" open="">
        <summary class="md-toc-link-wrapper">
          <a href="#共享内存" class="md-toc-link"><p>共享内存</p>
</a>
          </summary>
        <div>
          <div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#共享内存相关函数" class="md-toc-link">
            <p>共享内存相关函数</p>

          </a></div><div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#共享内存相关代码" class="md-toc-link">
            <p>共享内存相关代码</p>

          </a></div>
        </div>
      </details>
    <details style="padding:0;;padding-left:24px;" open="">
        <summary class="md-toc-link-wrapper">
          <a href="#信号量组" class="md-toc-link"><p>信号量组</p>
</a>
          </summary>
        <div>
          <div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#信号量组相关函数" class="md-toc-link">
            <p>信号量组相关函数</p>

          </a></div><div class="md-toc-link-wrapper" style="padding:0;;display:list-item;list-style:square;margin-left:42px">
          <a href="#信号量组相关代码" class="md-toc-link">
            <p>信号量组相关代码</p>

          </a></div>
        </div>
      </details>
    
        </div>
      </details>
    
</div>
</div>
      <a id="sidebar-toc-btn">≡</a>
    
    
    
    
    
    
<script>

var sidebarTOCBtn = document.getElementById('sidebar-toc-btn')
sidebarTOCBtn.addEventListener('click', function(event) {
  event.stopPropagation()
  if (document.body.hasAttribute('html-show-sidebar-toc')) {
    document.body.removeAttribute('html-show-sidebar-toc')
  } else {
    document.body.setAttribute('html-show-sidebar-toc', true)
  }
})
</script>
      
  
    </body></html>